Coder Social home page Coder Social logo

syyunn / tikzplotlib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nschloe/tikzplotlib

0.0 1.0 0.0 1.96 MB

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

Home Page: https://pypi.org/project/tikzplotlib

License: MIT License

Python 43.29% Makefile 0.21% TeX 56.50%

tikzplotlib's Introduction

tikzplotlib

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

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

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

pip3 install tikzplotlib --user

to install/update.

Usage

  1. Generate your matplotlib plot as usual.

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

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

tikzplotlib's People

Contributors

aikhjarto avatar awehrfritz avatar boeddeker avatar cassiersg avatar daniel-edler avatar danielhkl avatar dougnd avatar edmundus avatar eric-wieser avatar foucault avatar fsgeek avatar habi avatar haji-ali avatar harenbrs avatar hb avatar hgwd2 avatar janbrumm avatar lukasmu avatar m-rossi avatar madsadrian avatar maschlr avatar mlq avatar ngc92 avatar nschloe avatar olivierverdier avatar pwohlhart avatar samop avatar satejsoman avatar spott avatar stge4code avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.