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

yticklabels become weird with function matplotlib.pyplot.tight_layout()

Dothion opened this issue · comments

Here is my python code:

import tikzplotlib
import matplotlib.pyplot as plt
xs = [0, 315.55, 601.28, 956.15, 1205.98, 1514.03, 1804.015]
ys = [20, 12.6386, 8.71647, 3.90248, 1.76259, 0.637851, 0.622631]
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, sharex=True, sharey= True)
axes = (ax1, ax2, ax3, ax4)
for i in range(4):
    axes[i].plot(xs, ys, c=f'C{i}', marker = 'o')
fig.tight_layout() # <- bugs will disappear if del this line
# fig.savefig('fig.png')
tikzplotlib.save('fig.tex', encoding='utf8')

fig

And here is the pgf code generated by tikzplotlib:

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

\definecolor{color0}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
\definecolor{color1}{rgb}{1,0.498039215686275,0.0549019607843137}
\definecolor{color2}{rgb}{0.172549019607843,0.627450980392157,0.172549019607843}
\definecolor{color3}{rgb}{0.83921568627451,0.152941176470588,0.156862745098039}

\begin{groupplot}[group style={group size=2 by 2}]
\nextgroupplot[
scaled x ticks=manual:{}{\pgfmathparse{#1}},
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-90.20075, xmax=1894.21575,
xtick style={color=black},
xticklabels={},
y grid style={white!69.0196078431373!black},
ymin=-0.34623745, ymax=20.96886845,
ytick style={color=black},
ytick={-5,0,5,10,15,20,25},
yticklabels={−10,0,10,20,30,,} % <- ???
]
\addplot [semithick, color0, mark=*, mark size=3, mark options={solid}]
table {%
0 20
315.55 12.6386
601.28 8.71647
956.15 3.90248
1205.98 1.76259
1514.03 0.637851
1804.015 0.622631
};

\nextgroupplot[
scaled x ticks=manual:{}{\pgfmathparse{#1}},
scaled y ticks=manual:{}{\pgfmathparse{#1}},
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-90.20075, xmax=1894.21575,
xtick style={color=black},
xticklabels={},
y grid style={white!69.0196078431373!black},
ymin=-0.34623745, ymax=20.96886845,
ytick style={color=black},
yticklabels={}
]
\addplot [semithick, color1, mark=*, mark size=3, mark options={solid}]
table {%
0 20
315.55 12.6386
601.28 8.71647
956.15 3.90248
1205.98 1.76259
1514.03 0.637851
1804.015 0.622631
};

\nextgroupplot[
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-90.20075, xmax=1894.21575,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-0.34623745, ymax=20.96886845,
ytick style={color=black},
ytick={-5,0,5,10,15,20,25},
yticklabels={−10,0,10,20,30,,}
]
\addplot [semithick, color2, mark=*, mark size=3, mark options={solid}]
table {%
0 20
315.55 12.6386
601.28 8.71647
956.15 3.90248
1205.98 1.76259
1514.03 0.637851
1804.015 0.622631
};

\nextgroupplot[
scaled y ticks=manual:{}{\pgfmathparse{#1}},
tick align=outside,
tick pos=left,
x grid style={white!69.0196078431373!black},
xmin=-90.20075, xmax=1894.21575,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=-0.34623745, ymax=20.96886845,
ytick style={color=black},
yticklabels={}
]
\addplot [semithick, color3, mark=*, mark size=3, mark options={solid}]
table {%
0 20
315.55 12.6386
601.28 8.71647
956.15 3.90248
1205.98 1.76259
1514.03 0.637851
1804.015 0.622631
};
\end{groupplot}

\end{tikzpicture}

image

The y tick labels become different from matplotlib ones.