Matplotlib2LaTeX

We show here the code

mpl2latex.latex_figsize(wf=0.5, hf=0.6180339887498949, columnwidth=510)[source]

Get the correct figure size to be displayed in a latex report/publication

Parameters
  • wf (float, optional) -- width fraction in columnwidth units. Default to 0.5

  • hf (float, optional) -- height fraction in columnwidth units. Set by default to golden ratio.

  • columnwidth (float) -- width of the column in latex. Get this from LaTeX using showthecolumnwidth Default to 510

Returns

fig_size -- fig_size [width, height] that should be given to matplotlib

Return type

list of float

class mpl2latex.mpl2latex(back_flag, packages=None, backend='pgf', SMALL_SIZE=8, MEDIUM_SIZE=10, BIGGER_SIZE=11, BIGGEST_SIZE=12)[source]

Plot matplotlib figure in pgf for perfect LaTeX reports

Context to plot matplotlib figures using the pgf backend, swapping easily between the two. Simply put your plotting commands inside the context, and enable the pgf with the back_flag

back_flag

If True use the backend backend with all the details

Type

bool

SMALL_SIZE

Size of the font for default text sizes and tick labels. Default 8.

Type

float, optional

MEDIUM_SIZE

Font size for x-y labels and legend. Default 10.

Type

float, optional

BIGGER_SIZE

Font size for axes title. Default 11.

Type

float, optional

BIGGEST_SIZE

Font size fot the figure title. Default 12.

Type

float, optional

packages

LaTeX packages to use, in the form "usepackage[options]{package}". If None only "usepackage[utf8]{inputenc}" is used.

Type

list of strings, optional

original_backend

Original backend when the class is initialized

Type

string

original_rcParams

Original rcParams when the class is initialized

Type

matplotlib.RcParams

backend

Matplotlib backend to use. Default is pgf

Type

string, optional

def __init__(self, back_flag, packages = None, backend='pgf'):

Initialize the class.

Parameters
  • back_flag (bool) --

  • True use the backend backend with all the details (If) --

  • packages (list of strings, optional) -- LaTeX packages to use, in the form "usepackage[options]{package}". If None only "usepackage[utf8]{inputenc}" is used.

  • original_backend (string) -- Original backend when the class is initialized

  • original_rcParams (matplotlib.RcParams) -- Original rcParams when the class is initialized

  • backend (string, optional) -- Matplotlib backend to use. Default is pgf

__enter__(self)[source]

enter in the context

__exit__(self)[source]

exit from the context

Examples

>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> x = np.linspace(0, 100, 1000)
>>> flag = True
>>> # Write the usual plotting instruction inside the context
>>> with mpl2ltx(flag):
>>>     plt.plot(x, x**2)
>>>     plt.savefig('trial.pgf')
>>> # produces the plots and save it in pgf as intended