All Projects → nschloe → Tikzplotlib

nschloe / Tikzplotlib

Licence: mit
Convert matplotlib figures to TikZ/PGFplots for smooth integration into LaTeX.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Tikzplotlib

Neural
LATEX: TikZ package for drawing neural networks. Also available on CTAN at http://www.ctan.org/tex-archive/graphics/pgf/contrib/neuralnetwork
Stars: ✭ 169 (-87.37%)
Mutual labels:  latex, tikz
Tikz
Galley of Tikz drawings.
Stars: ✭ 251 (-81.24%)
Mutual labels:  latex, tikz
Sane tikz
Reconquer the canvas: beautiful Tikz figures without clunky Tikz code
Stars: ✭ 196 (-85.35%)
Mutual labels:  latex, tikz
Tikzcd Editor
A simple visual editor for creating commutative diagrams.
Stars: ✭ 1,627 (+21.6%)
Mutual labels:  latex, tikz
Awesome Tikz
A curated list of awesome TikZ documentations, libraries and resources
Stars: ✭ 804 (-39.91%)
Mutual labels:  latex, tikz
Latex Cheatsheet
Template for a compact LaTeX Cheatsheet I made some years ago.
Stars: ✭ 136 (-89.84%)
Mutual labels:  latex, tikz
Ktikz
KtikZ provides a nice user interface for making pictures using TikZ.
Stars: ✭ 233 (-82.59%)
Mutual labels:  latex, tikz
Quiver
A modern commutative diagram editor for the web.
Stars: ✭ 1,799 (+34.45%)
Mutual labels:  latex, tikz
Tikz
Complete collection of my PGF/TikZ figures.
Stars: ✭ 668 (-50.07%)
Mutual labels:  latex, tikz
tikz favorites
collection of favorite TikZ graphics
Stars: ✭ 62 (-95.37%)
Mutual labels:  latex, tikz
Tikz Feynman
Feynman Diagrams with TikZ
Stars: ✭ 104 (-92.23%)
Mutual labels:  latex, tikz
Latex Examples
Examples for the usage of LaTeX
Stars: ✭ 1,032 (-22.87%)
Mutual labels:  latex, tikz
Circuitikz
CircuiTikZ TeX/LaTeX package for drawing circuits
Stars: ✭ 222 (-83.41%)
Mutual labels:  latex, tikz
lazylatex
Because LaTeX shouldn't be boring! 🐠 LaTeX package inspired by sphinx-rtd-theme. Build with tcolorbox, minted, tikz, etc,.
Stars: ✭ 16 (-98.8%)
Mutual labels:  latex, tikz
Tikz drawings
Drawing in LaTeX
Stars: ✭ 18 (-98.65%)
Mutual labels:  latex, tikz
Matlab2tikz
This program converts MATLAB®/Octave figures to TikZ/pgfplots figures for smooth integration into LaTeX.
Stars: ✭ 1,041 (-22.2%)
Mutual labels:  latex, tikz
Cookietemple
A collection of best practice cookiecutter templates for all domains and languages with extensive Github support
Stars: ✭ 81 (-93.95%)
Mutual labels:  latex
Texstudio
TeXstudio is a fully featured LaTeX editor. Our goal is to make writing LaTeX documents as easy and comfortable as possible.
Stars: ✭ 1,300 (-2.84%)
Mutual labels:  latex
Heckle
✒️ Jekyll in Haskell (feat. LaTeX)
Stars: ✭ 80 (-94.02%)
Mutual labels:  latex
Excel2latex
Javascript .xlsx to LaTeX table converter
Stars: ✭ 79 (-94.1%)
Mutual labels:  latex

tikzplotlib

The artist formerly known as matplotlib2tikz.

PyPi Version Packaging status PyPI pyversions DOI GitHub stars PyPi downloads

Discord Documentation Status awesome

gh-actions codecov LGTM Code style: black

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

for native inclusion into LaTeX or ConTeXt documents.

The output of tikzplotlib is in PGFPlots, a TeX 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

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 tikzplotlib

tikzplotlib.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.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 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 TeX 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

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

pip install tikzplotlib

to install.

Usage

  1. Generate your matplotlib plot as usual.

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

    import tikzplotlib
    tikzplotlib.save("mytikz.tex")
    # or
    tikzplotlib.save("mytikz.tex", flavor="context")
    

    to store the TikZ file as mytikz.tex.

  3. Add the contents of mytikz.tex into your TeX 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{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:

    import tikzplotlib
    tikzplotlib.Flavors.latex.preamble()
    # or
    tikzplotlib.Flavors.context.preamble()
    
  4. Optional: clean up the figure before exporting to tikz using the clean_figure command.

    import matplotlib.pyplot as plt
    import numpy as np
    
    #... do your plotting
    
    import tikzplotlib
    tikzplotlib.clean_figure()
    tikzplotlib.save("test.tex")
    

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

    The feature originated from the matlab2tikz project and is adapted to matplotlib.

Contributing

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.

Testing

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

pytest

License

tikzplotlib is published under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].