tikzplotlib

raw JSON →
0.10.1 verified Fri May 01 auth: no python

tikzplotlib is a Python tool that converts matplotlib figures into TikZ/PGFPlots code, enabling LaTeX-friendly vector graphics. Current version 0.10.1, requires Python >=3.7. Released irregularly.

pip install tikzplotlib
error ModuleNotFoundError: No module named 'tikzplotlib'
cause Library not installed or pip install failed.
fix
Run 'pip install tikzplotlib' and ensure the environment is correct.
error AttributeError: module 'tikzplotlib' has no attribute 'save'
cause Version mismatch or incorrect import path.
fix
Use 'import tikzplotlib' and call 'tikzplotlib.save()'. For versions <0.9.0, use 'from tikzplotlib import save as tikz_save'.
error TypeError: save() got multiple values for argument 'filepath'
cause Passing both positional and keyword argument for filepath.
fix
Provide filepath as positional: 'tikzplotlib.save('output.tex')' or keyword: 'tikzplotlib.save(filepath='output.tex')'.
error ValueError: Cannot save an empty figure
cause No figure has been created or the figure has been closed.
fix
Create a figure with 'plt.figure()' or 'plt.plot()' before calling save.
breaking In tikzplotlib 0.10.x, the function 'tikzplotlib.save()' now returns the TikZ code as a string if no filename is given. Previously, it always wrote to a file. This can break scripts that do not expect a return value.
fix Assign the return value or check if None when providing a filename. Use 'return_str=False' to suppress string output.
deprecated The module 'tikzplotlib.save' is deprecated since 0.9.0. Use 'tikzplotlib.save()' directly (the function, not a submodule).
fix Replace 'from tikzplotlib.save import ...' with 'import tikzplotlib' and call 'tikzplotlib.save(...)'.
gotcha tikzplotlib requires matplotlib to be installed and the figure to be fully rendered before calling save. Otherwise, the output may be incomplete or raise AttributeError.
fix Always call plt.show() or ensure the figure is properly closed before saving, or use tikzplotlib with 'plt.gcf()'.

Convert a matplotlib figure to a TikZ file using tikzplotlib.save().

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
plt.plot(x, np.sin(x))
plt.title('Simple sine wave')
tikzplotlib.save('sine_wave.tex')