TransWikia.com

Best matplotlitb to Tikz conversion tool

TeX - LaTeX Asked by rhz on September 30, 2021

What are some good, well-maintained packages for converting matplotlib plots to Tikz files for inclusion in latex documents?

2 Answers

matplotlib supports to export to PGF (the graphics language behind TikZ) or to directly built the plot via LaTeX and save it as a PDF.

To use this just use the following in your Python code:

import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.use("pgf")
# create your plot
plt.savefig("file.pgf") # save as PGF file which can be used in your document via `input`
plt.savefig("file.pdf") # save as PDF created with LaTeX

You can customize the output of the PDF/PGF via mpl.rcParams.update(), which expects a dictionary as argument with which you can set several interesting parameters for the created PDF/PGF:

{
    "pgf.texsystem":   "pdflatex", # or any other engine you want to use
    "text.usetex":     True,       # use TeX for all texts
    "font.family":     "serif",
    "font.serif":      [],         # empty entries should cause the usage of the document fonts
    "font.sans-serif": [],
    "font.monospace":  [],
    "font.size":       10,         # control font sizes of different elements
    "axes.labelsize":  10,
    "legend.fontsize": 9,
    "xtick.labelsize": 9,
    "ytick.labelsize": 9,
    "pgf.preamble": [              # specify additional preamble calls for LaTeX's run
        r"usepackage[T1]{fontenc}",
        r"usepackage{siunitx}",
    ],
}

Answered by Skillmon on September 30, 2021

tikzplotlib (formerly matplotlib2tikz) creates LaTeX code using the pgfplots package. It's available on Pypi, so it can be installed with pip.

Basically do

import tikzplotlib

and then

tikzplotlib.save('filename.tex')

after generating a figure. In your LaTeX-file add

usepackage{pgfplots}

to the preamble, and

input{filename}

to add the figure.

The tikzplotlib.save function has multiple options for modifying the code, so have a look at its docstring.

Answered by Torbjørn T. on September 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP