thomas-saigre / tikzplotly

Export plotly figures as TikZ/PGFplots for integration in LaTeX
https://pypi.org/project/tikzplotly/
MIT License
16 stars 0 forks source link
latex plotly python tikz

logo-tikzplotly

PyPi Version PyPI pyversions

Issue codecov GitHub Actions Workflow Status

Tikzplotly

Convert plotly figures to tikz code for inclusion into PGFPlots (PGF/TikZ) figures.

This results in a tikz code, that can be easily included into your LaTeX document. This also allows to easily edit the content of the figure.

Example

The following Python code

import plotly.express as px
import tikzplotly

df = px.data.gapminder().query("continent == 'Oceania'")
fig = px.line(df, x='year', y='lifeExp', color='country', markers=True)
tikzplotly.save("example.tex", fig)

will result in the following tikz code

\pgfplotstableread{data0 Australia New_Zealand
1952 69.12 69.39
1957 70.33 70.26
1962 70.93 71.24
1967 71.1 71.52
1972 71.93 71.89
1977 73.49 72.22
1982 74.74 73.84
1987 76.32 74.32
1992 77.56 76.33
1997 78.83 77.55
2002 80.37 79.11
2007 81.235 80.204
}\dataZ

\begin{tikzpicture}

\definecolor{636efa}{HTML}{636efa}
\definecolor{EF553B}{HTML}{EF553B}

\begin{axis}[
xlabel=year,
ylabel=lifeExp,
]
\addplot+ [mark=*, solid, color=636efa, mark options={solid, draw=636efa}] table[y=Australia] {\dataZ};
\addlegendentry{Australia}
\addplot+ [mark=*, solid, color=EF553B, mark options={solid, draw=EF553B}] table[y=New_Zealand] {\dataZ};
\addlegendentry{New Zealand}
\end{axis}
\end{tikzpicture}

Installation

Tikzplotly is available from the Python Package Index, so it can be installed with pip :

pip install tikzplotly

Usage

  1. Generate the figure with Plotly,
  2. Invoke tikzplotly to convert the figure to tikz code :
    
    import tikzplotly

tikzplotly.save("figure.tex", fig)

3. Add the content of the generated file `figure.tex` to your LaTeX document :
```latex
\input{figure.tex}

To correctly compile the document, you will need to add the following packages to your preamble :

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

Note

References

License

This project is licensed under the MIT License.