Skip to main content

Convert matplotlib figures into TikZ/PGFPlots

Project description

matplotlib2tikz

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

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

for native inclusion into LaTeX documents.

The output of matplotlib2tikz 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 matplotlib2tikz

  • 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

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}

(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.

Installation

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

pip3 install matplotlib2tikz --user

to install/update.

Usage

  1. Generate your matplotlib plot as usual.

  2. Instead of pyplot.show(), invoke matplotlib2tikz by

    import matplotlib2tikz
    matplotlib2tikz.save("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

    \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}
    

Contributing

If 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 visit matplotlib2tikz's GitHub page.

Testing

matplotlib2tikz 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 matplotlib2tikz and compare the output with a previously stored reference TeX file.

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

pytest

Distribution

To create a new release

  1. bump the __version__ number,

  2. publish to PyPi and GitHub:

    $ make publish
    

License

matplotlib2tikz 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

matplotlib2tikz-0.7.4.tar.gz (45.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matplotlib2tikz-0.7.4-py2.py3-none-any.whl (36.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file matplotlib2tikz-0.7.4.tar.gz.

File metadata

  • Download URL: matplotlib2tikz-0.7.4.tar.gz
  • Upload date:
  • Size: 45.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for matplotlib2tikz-0.7.4.tar.gz
Algorithm Hash digest
SHA256 067927ab3c820e5ce55d73e626198306c3ff0c8d2dfdbc8ddca5eeb24a81907f
MD5 4179fed8659ed67ed8af83704a65a60e
BLAKE2b-256 310121760d832ca5264f2cecf949336327e502ca5847c08b70620af2e2b02f27

See more details on using hashes here.

File details

Details for the file matplotlib2tikz-0.7.4-py2.py3-none-any.whl.

File metadata

  • Download URL: matplotlib2tikz-0.7.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 36.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for matplotlib2tikz-0.7.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 35c116f5c4dda98f65c3dc4c305b1ef24cfbb1b8b5b3e1561d709357edc79723
MD5 20a65345e6badfdd16fb4cc03b593fa1
BLAKE2b-256 12336beea67fdd9763554bc21a87f597152a0ac81a06a337e80612c87ab3388e

See more details on using hashes here.

Supported by

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