nschloe / tikzplotlib

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TeX code changes after calling pyplot.savefig()

MaxGaukler opened this issue · comments

Calling plt.savefig() changes the tikzplotlib result. A minimized example is the following. Complicated examples of this bug can be found in #416 and #400 .

from matplotlib import pyplot as plt
import tikzplotlib
import numpy as np
plt.plot([-1, 0])
code_before = tikzplotlib.get_tikz_code()
plt.savefig("test.pdf")
code_after = tikzplotlib.get_tikz_code()
print(code_before == code_after)
if code_before != code_after:
    print("===")
    print(code_before)
    print("===")
    print(code_after)
    print("===")

Differences in the result:

--- before
+++ after
@@ -9,9 +9,13 @@
 x grid style={white!69.0196078431373!black},
 xmin=-0.05, xmax=1.05,
 xtick style={color=black},
+xtick={-0.2,0,0.2,0.4,0.6,0.8,1,1.2},
+xticklabels={−0.2,0.0,0.2,0.4,0.6,0.8,1.0,1.2},
 y grid style={white!69.0196078431373!black},
 ymin=-1.05, ymax=0.05,
-ytick style={color=black}
+ytick style={color=black},
+ytick={-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2},
+yticklabels={−1.2,−1.0,−0.8,−0.6,−0.4,−0.2,0.0,0.2}
 ]
 \addplot [semithick, color0]
 table {%
Full result
False
===
% This file was created by tikzplotlib v0.9.2.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.05, xmax=1.05,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-1.05, ymax=0.05,
ytick style={color=black}
]
\addplot [semithick, color0]
table {%
0 -1
1 0
};
\end{axis}

\end{tikzpicture}

===
% This file was created by tikzplotlib v0.9.2.
\begin{tikzpicture}

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}

\begin{axis}[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-0.05, xmax=1.05,
xtick style={color=black},
xtick={-0.2,0,0.2,0.4,0.6,0.8,1,1.2},
xticklabels={−0.2,0.0,0.2,0.4,0.6,0.8,1.0,1.2},
y grid style={white!69.0196078431373!black},
ymin=-1.05, ymax=0.05,
ytick style={color=black},
ytick={-1.2,-1,-0.8,-0.6,-0.4,-0.2,0,0.2},
yticklabels={−1.2,−1.0,−0.8,−0.6,−0.4,−0.2,0.0,0.2}
]
\addplot [semithick, color0]
table {%
0 -1
1 0
};
\end{axis}

\end{tikzpicture}

===

This is normal. plt.show() and plt.savefig() change the figure data in subtle ways. If anything, I'd call this an mpl bug.