Skip to main content

Convert matplotlib figures into TikZ/PGFPlots

Project description


CircleCI codecov Code style: black Documentation Status awesome PyPi Version DOI GitHub stars PyPi downloads

This is tikzplotlib, a Python tool for converting matplotlib figures into PGFPlots (PGF/TikZ) figures like

for native inclusion into LaTeX documents.

The output of tikzplotlib is in PGFPlots, a LaTeX library that sits on top of PGF/TikZ and describes graphs in terms of axes, data etc. Consequently, the output of tikzplotlib

  • retains more information,
  • can be more easily understood, and
  • is more easily editable

than raw TikZ output. For example, the matplotlib figure

import matplotlib.pyplot as plt
import numpy as np"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}$")

import tikzplotlib"test.tex")

(see above) gives



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)},
xmin=-0.095, xmax=1.995,
y grid style={white},
ylabel={Voltage (mV)},
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


(Use get_tikz_code() instead of save() 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 manual contains 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.


tikzplotlib is available from the Python Package Index, so simply type

pip3 install tikzplotlib --user

to install/update.


  1. Generate your matplotlib plot as usual.

  2. Instead of, invoke tikzplotlib by

    import tikzplotlib"mytikz.tex")

    to store the TikZ file as mytikz.tex.

  3. Add the contents of mytikz.tex into your LaTeX source code. A convenient way of doing so is via


    Also make sure that the packages for PGFPlots and proper Unicode support and are included in the header of your document:

    \usepackage{fontspec}  % optional


If you experience bugs, would like to contribute, have nice examples of what tikzplotlib can do, or if you are just looking for more information, then please visit tikzplotlib's GitHub page.


tikzplotlib has automatic unit testing to make sure that the software doesn't accidentally get worse over time. In test/, a number of test cases are specified. Those run through tikzplotlib and compare the output with a previously stored reference TeX file.

To run the tests, just check out this repository and type



tikzplotlib is published under the MIT license.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tikzplotlib-0.8.1.tar.gz (45.9 kB view hashes)

Uploaded Source

Built Distribution

tikzplotlib-0.8.1-py3-none-any.whl (36.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page