Package: matplotlib2tikz
matplotlib2tikz has been renamed totikzplotlib. matplotlib2tikz will no longer be supported.matplotlib2tikzThis is matplotlib2tikz, a Python tool for converting matplotlib figures intoPGFPlots(PGF/TikZ)
figures likefor native inclusion into LaTeX documents.The output of matplotlib2tikz is inPGFPlots, a LaTeX library that sits on
top ofPGF/TikZand describes graphs in terms
of axes, data etc. Consequently, the output of matplotlib2tikzretains more information,can be more easily understood, andis more easily editablethanraw TikZ output.
For example, the matplotlib figureimport matplotlib.pyplot as plt
import numpy as np

plt.style.use("ggplot")

t = np.arange(0.0, 2.0, 0.1)
s = np.sin(2 * np.pi * t)
s2 = np.cos(2 * np.pi * t)
plt.plot(t, s, "o-", lw=4.1)
plt.plot(t, s2, "o-", lw=4.1)
plt.xlabel("time (s)")
plt.ylabel("Voltage (mV)")
plt.title("Simple plot $\\frac{\\alpha}{2}$")
plt.grid(True)

import matplotlib2tikz

matplotlib2tikz.save("test.tex")(see above) gives\begin{tikzpicture}\definecolor{color0}{rgb}{0.886274509803922,0.290196078431373,0.2}\definecolor{color1}{rgb}{0.203921568627451,0.541176470588235,0.741176470588235}\begin{axis}[
axis background/.style={fill=white!89.80392156862746!black},
axis line style={white},
tick align=outside,
tick pos=left,
title={Simple plot$\frac{\alpha}{2}$},
x grid style={white},
xlabel={time (s)},
xmajorgrids,
xmin=-0.095, xmax=1.995,
y grid style={white},
ylabel={Voltage (mV)},
ymajorgrids,
ymin=-1.1, ymax=1.1
]\addplot[line width=1.64pt, color0, mark=*, mark size=3, mark options={solid}]
table{%0 0
0.1 0.587785252292473% [...]1.9 -0.587785252292473};\addplot[line width=1.64pt, color1, mark=*, mark size=3, mark options={solid}]
table{%0 1
0.1 0.809016994374947% [...]1.9 0.809016994374947};\end{axis}\end{tikzpicture}(Useget_tikz_code()instead ofsave()if you want the code as a string.)Tweaking the plot is straightforward and can be done as part of your LaTeX work flow.The fantastic PGFPlots manualcontains
great examples of how to make your plot look even better.Of course, not all figures produced by matplotlib can be converted without error.
Notably,3D plots don't work.Installationmatplotlib2tikz isavailable from the Python Package
Index, so simply typepip3 install matplotlib2tikz --userto install/update.UsageGenerate your matplotlib plot as usual.Instead ofpyplot.show(), invoke matplotlib2tikz byimportmatplotlib2tikzmatplotlib2tikz.save("mytikz.tex")to store the TikZ file asmytikz.tex.Add the contents ofmytikz.texinto your LaTeX source code. A convenient way of
doing so is via\input{/path/to/mytikz.tex}Also make sure that the packages for PGFPlots and proper Unicode support and are
included in the header of your document:\usepackage[utf8]{inputenc}\usepackage{fontspec}% optional\usepackage{pgfplots}\pgfplotsset{compat=newest}\usepgfplotslibrary{groupplots}\usepgfplotslibrary{dateplot}ContributingIf you experience bugs, would like to contribute, have nice examples of what
matplotlib2tikz can do, or if you are just looking for more information, then please
visitmatplotlib2tikz's GitHub page.Testingmatplotlib2tikz has automatic unit testing to make sure that the software doesn't
accidentally get worse over time. Intest/, a number of test cases are specified.
Those run through matplotlib2tikz and compare the output with a previously stored
reference TeX file.To run the tests, just check out this repository and typepytestDistributionTo create a new releasebump the__version__number,publish to PyPi and GitHub:$ make publishLicensematplotlib2tikz is published under theMIT license.