Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

📊 Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.

License

NotificationsYou must be signed in to change notification settings

eqnmapper/tikzplotlib

 
 

Repository files navigation

tikzplotlib

The artist formerly known asmatplotlib2tikz.

PyPi VersionPackaging statusPyPI pyversionsDOIGitHub starsDownloads

Documentation Statusawesome

gh-actionscodecovLGTMCode style: black

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

for native inclusion into LaTeX or ConTeXt documents.

The output of tikzplotlib is inPGFPlots, a TeXlibrary that sits on top ofPGF/TikZ anddescribes 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

thanraw TikZ output.For example, the matplotlib figure

importmatplotlib.pyplotaspltimportnumpyasnpplt.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)importtikzplotlibtikzplotlib.save("test.tex")
importmatplotlibasmplplt.close()mpl.rcParams.update(mpl.rcParamsDefault)

-->(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.8039215686275!black},axis line style={white},tick align=outside,tick pos=left,title={Simple plot\(\displaystyle\frac{\alpha}{2}\)},x grid style={white},xlabel={time (s)},xmajorgrids,xmin=-0.095, xmax=1.995,xtick style={color=white!33.3333333333333!black},y grid style={white},ylabel={Voltage (mV)},ymajorgrids,ymin=-1.1, ymax=1.1,ytick style={color=white!33.3333333333333!black}]\addplot [line width=1.64pt, color0, mark=*, mark size=3, mark options={solid}]table {%0 00.1 0.587785252292473% [...]1.9 -0.587785252292473};\addplot [line width=1.64pt, color1, mark=*, mark size=3, mark options={solid}]table {%0 10.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 TeX work flow.The fantastic PGFPlots manual containsgreat 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

tikzplotlib isavailable from the Python PackageIndex, so simply do

pip install tikzplotlib

to install.

Usage

  1. Generate your matplotlib plot as usual.

  2. Instead ofpyplot.show(), invoke tikzplotlib by

    importtikzplotlibtikzplotlib.save("mytikz.tex")# ortikzplotlib.save("mytikz.tex",flavor="context")

    to store the TikZ file asmytikz.tex.

  3. Add the contents ofmytikz.tex into your TeX source code. A convenient way of doingso is via

    \input{/path/to/mytikz.tex}

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

    \usepackage[utf8]{inputenc}\usepackage{pgfplots}\DeclareUnicodeCharacter{2212}{−}\usepgfplotslibrary{groupplots,dateplot}\usetikzlibrary{patterns,shapes.arrows}\pgfplotsset{compat=newest}

    or:

    \setupcolors[state=start]\usemodule[tikz]\usemodule[pgfplots]\usepgfplotslibrary[groupplots,dateplot]\usetikzlibrary[patterns,shapes.arrows]\pgfplotsset{compat=newest}\unexpanded\def\startgroupplot{\groupplot}\unexpanded\def\stopgroupplot{\endgroupplot}

    You can also get the code via:

    importtikzplotlibtikzplotlib.Flavors.latex.preamble()# ortikzplotlib.Flavors.context.preamble()
  4. [Optional] Clean up the figure before exporting to tikz using theclean_figurecommand.

    importmatplotlib.pyplotaspltimportnumpyasnp# ... do your plottingimporttikzplotlibtikzplotlib.clean_figure()tikzplotlib.save("test.tex")

    The command will remove points that are outside the axes limits, simplify curves andreduce point density for the specified target resolution.

    The feature originated from thematlab2tikz project and is adapted tomatplotlib.

Contributing

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

Testing

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

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

pytest

License

tikzplotlib is published under theMITlicense.

About

📊 Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python66.7%
  • TeX33.3%

[8]ページ先頭

©2009-2025 Movatter.jp