Contents:

# Methods¶

Script to convert Matplotlib generated figures into TikZ/PGFPlots figures.

tikzplotlib.get_tikz_code(figure='gcf', filepath=None, axis_width=None, axis_height=None, textsize=10.0, tex_relative_path_to_data=None, externalize_tables=False, override_externals=False, strict=False, wrap=True, add_axis_environment=True, extra_axis_parameters=None, extra_groupstyle_parameters={}, extra_tikzpicture_parameters=None, dpi=None, show_info=False, include_disclaimer=True, standalone=False, float_format='.15g', table_row_sep='\n', flavor='latex')

Main function. Here, the recursion into the image starts and the contents are picked up. The actual file gets written in this routine.

Parameters: figure – either a Figure object or ‘gcf’ (default). axis_width (str) – If not None, this will be used as figure width within the TikZ/PGFPlots output. If axis_height is not given, tikzplotlib will try to preserve the original width/height ratio. Note that axis_width can be a string literal, such as '\axis_width'. axis_height (str) – If not None, this will be used as figure height within the TikZ/PGFPlots output. If axis_width is not given, tikzplotlib will try to preserve the original width/height ratio. Note that axis_width can be a string literal, such as '\axis_height'. textsize (float) – The text size (in pt) that the target latex document is using. Default is 10.0. tex_relative_path_to_data (str) – In some cases, the TikZ file will have to refer to another file, e.g., a PNG for image plots. When \input into a regular LaTeX document, the additional file is looked for in a folder relative to the LaTeX file, not the TikZ file. This arguments optionally sets the relative path from the LaTeX file to the data. externalize_tables (bool) – Whether or not to externalize plot data tables into tsv files. override_externals (bool) – Whether or not to override existing external files (such as tsv or images) with conflicting names (the alternative is to choose other names). strict (bool) – Whether or not to strictly stick to matplotlib’s appearance. This influences, for example, whether tick marks are set exactly as in the matplotlib plot, or if TikZ/PGFPlots can decide where to put the ticks. wrap (bool) – Whether '\begin{tikzpicture}'/'\starttikzpicture' and '\end{tikzpicture}'/'\stoptikzpicture' will be written. One might need to provide custom arguments to the environment (eg. scale= etc.). Default is True. add_axis_environment (bool) – Whether '\begin{axis}[...]'/’startaxis[…]’ and '\end{axis}'/'\stopaxis' will be written. One needs to set the environment in the document. If False additionally sets wrap=False. Default is True. extra_axis_parameters (a list or set of strings for the pfgplots axes.) – Extra axis options to be passed (as a list or set) to pgfplots. Default is None. extra_tikzpicture_parameters (a set of strings for the pfgplots tikzpicture.) – Extra tikzpicture options to be passed (as a set) to pgfplots. dpi (int) – The resolution in dots per inch of the rendered image in case of QuadMesh plots. If None it will default to the value savefig.dpi from matplotlib.rcParams. Default is None. show_info (bool) – Show extra info on the command line. Default is False. include_disclaimer (bool) – Include tikzplotlib disclaimer in the output. Set False to make tests reproducible. Default is True. standalone (bool) – Include wrapper code for a standalone LaTeX file. float_format (str) – Format for float entities. Default is ".15g". table_row_sep (str) – Row separator for table data. Default is "\n". flavor (str) – TeX flavor of the output code. Supported are "latex" and”context”. Default is "latex". None

The following optional attributes of matplotlib’s objects are recognized and handled:

• axes.Axes._tikzplotlib_anchors This attribute can be set to a list of ((x,y), anchor_name) tuples. Invisible nodes at the respective location will be created which can be referenced from outside the axis environment.
tikzplotlib.save(filepath, *args, encoding=None, **kwargs)

Same as get_tikz_code(), but actually saves the code to a file.

Parameters: filepath (str) – The file to which the TikZ output will be written. encoding – Sets the text encoding of the output file, e.g. ‘utf-8’. For supported values: see codecs module. None
tikzplotlib.clean_figure(fig=None, target_resolution=600, scale_precision=1.0)
Cleans figure as a preparation for tikz export.

This will minimize the number of points required for the tikz figure. If the figure has subplots, it will recursively clean then up.

Note that this function modifies the figure directly (impure function).

param fig: param target_resolution: Matplotlib figure handle (Default value = None) target resolution of final figure in PPI. If a scalar integer is provided, it is assumed to be square in both axis. If a list or an np.array is provided, it is interpreted as [H, W]. By default 600 int, list or np.array, optional scalar value indicating precision when scaling down. By default 1 float, optional

1. 2D lineplot python

from tikzplotlib import get_tikz_code, cleanfigure

x = np.linspace(1, 100, 20) y = np.linspace(1, 100, 20)

with plt.rc_context(rc=RC_PARAMS):

fig, ax = plt.subplots(1, 1, figsize=(5, 5)) ax.plot(x, y) ax.set_ylim([20, 80]) ax.set_xlim([20, 80]) raw = get_tikz_code()

clean_figure(fig) clean = get_tikz_code()

# Use number of lines to test if it worked. # the baseline (raw) should have 20 points # the clean version (clean) should have 2 points # the difference in line numbers should therefore be 2 numLinesRaw = raw.count(“

“)
numLinesClean = clean.count(“
“)
print(“number of tikz lines saved”, numLinesRaw - numLinesClean)

2. 3D lineplot python

from tikzplotlib import get_tikz_code, cleanfigure

theta = np.linspace(-4 * np.pi, 4 * np.pi, 100) z = np.linspace(-2, 2, 100) r = z ** 2 + 1 x = r * np.sin(theta) y = r * np.cos(theta)

with plt.rc_context(rc=RC_PARAMS):

fig = plt.figure() ax = fig.add_subplot(111, projection=”3d”) ax.plot(x, y, z) ax.set_xlim([-2, 2]) ax.set_ylim([-2, 2]) ax.set_zlim([-2, 2]) ax.view_init(30, 30) raw = get_tikz_code(fig)

clean_figure(fig) clean = get_tikz_code()

# Use number of lines to test if it worked. numLinesRaw = raw.count(“

“)
numLinesClean = clean.count(“
“)
assert numLinesRaw - numLinesClean == 14
class tikzplotlib.Flavors

An enumeration.