nschloe / tikzplotlib

:bar_chart: Save matplotlib figures as TikZ/PGFplots for smooth integration into LaTeX.
MIT License
2.42k stars 218 forks source link

Axis label are not in the same precision as original plot #142

Open lucaventurini opened 7 years ago

lucaventurini commented 7 years ago

When exporting this simple plot:

import matplotlib.pyplot as plt

plt.ylim([0.635, 0.665])
plt.grid()

the label on the y axis in the result are labeled to 2-digit precision. I'd expect a behavior similar to matplotlib, i.e. 3-digit precision.

nschloe commented 7 years ago

Can you post the output tex too? Shouldn't be too large in this case.

lucaventurini commented 7 years ago

Sure.

% This file was created by matplotlib2tikz v0.6.2.
\begin{tikzpicture}

\begin{axis}[
xmin=0, xmax=1,
ymin=0.635, ymax=0.665,
tick align=outside,
xmajorgrids,
x grid style={lightgray!92.026143790849673!black},
ymajorgrids,
y grid style={lightgray!92.026143790849673!black}
]
\end{axis}

\end{tikzpicture}
nschloe commented 7 years ago

Alright, so that looks fairly normal.

What happens here is that default settings from matplotlib are translated into default settings in pgfplots. Those simply happen to differ.

Trying to imitate exactly what mpl looks like would probably make the output file a mess. (Think of legend border colors, fonts, etc.)

What you're looking for is the extra argument to save(), and you want to supply a pgfplots command for specifying the data precision. Not sure what that is right now, but you'll find it in the manual.

lucaventurini commented 7 years ago

Seems fair.

Now let's say I add:

fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(FormatStrFormatter('%.3f'))

to the plot above. Shouldn't be it translated to pgfplots? (it is not...)

nschloe commented 7 years ago

Hm, yeah, the number formats aren't considered at all. Perhaps this can be fixed after all...