Commit b5b57422 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Add the module matplotlib_tools.

parent 32345cc8
# -*- coding: utf-8 -*-
"""matplotlib_tools module
"""
import matplotlib as mpl
import os
from datetime import datetime
from gluon import current
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.ticker import IndexLocator
NOW = datetime.now()
def create_pdf(base):
"""Generate the pdf file. It name depends on the user criteria and on
the arguments.
Args:
base (str): the filename is base.pdf
Returns:
matplotlib.backends.backend_pdf.PdfPages
"""
request = current.request
fn = "%s.pdf" % base
fn = os.path.join("applications", request.application, "scripts", fn)
return PdfPages(fn)
def header_footer(fig, title):
"""write the header_footer and the footer.
Args:
ax (matplotlib.Figure):
title (str):
"""
msg = NOW.strftime("%b %d, %Y %H:%M\nday %j -- week %W")
fig.text(0.05, 0.9, title, fontsize=14)
fig.text(0.94, 0.89, msg, fontsize=8, horizontalalignment='right')
fig.text(0.94, 0.04, fig.number, fontsize=10, horizontalalignment='right')
def style():
"""Define the matplotlib style, namely font size, line width, etc.
"""
mpl.rcParams['axes.labelsize'] = 9
mpl.rcParams['figure.figsize'] = (29.7 / 2.54, 21 / 2.54)
mpl.rcParams['figure.subplot.top'] = 0.85
mpl.rcParams['legend.fontsize'] = 7
mpl.rcParams['lines.linewidth'] = 1.
mpl.rcParams['xtick.major.size'] = 8
mpl.rcParams['xtick.minor.size'] = 4
mpl.rcParams['xtick.labelsize'] = 8
mpl.rcParams['ytick.major.size'] = 8
mpl.rcParams['ytick.minor.size'] = 4
mpl.rcParams['ytick.labelsize'] = 8
def ticks_and_labels(ax, xlabel="", ylabel=""):
"""Set ylabel and activate minor ticks.
Args:
ax (matplotlib.Axes):
xlabel (str):
ylabel (str):
"""
ax.minorticks_on()
ax.set_xlabel(xlabel, horizontalalignment='right', x=1.)
ax.set_ylabel(ylabel, horizontalalignment='right', y=1.)
def xticks_and_labels(ax, xticks, xlabel="", ylabel="", base=1., offset=0.5):
"""Set ylabel and activate minor ticks.
Args:
ax (matplotlib.Axes):
xticks (list):
xlabel (str):
ylabel (str):
base (int): base for the IndexLocator
offset (float): offset for the IndexLocator
"""
ax.set_xlabel(xlabel, horizontalalignment='right', x=1.)
ax.set_ylabel(ylabel, horizontalalignment='right', y=1.)
ax.xaxis.set_major_locator(IndexLocator(base, offset))
ax.xaxis.set_ticklabels(xticks)
ax.minorticks_on()
ax.xaxis.set_tick_params(which='minor', size=0)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment