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

tikzplotlib.save results in a "Dimension too large" error

mildblimp opened this issue · comments

This happens when trying to plot a figure where the axes are scaled, for example, when the y-axis values are very close to each other. See the following minimal working example.

import matplotlib.pyplot as plt
from tikzplotlib import save


fig, ax = plt.subplots()
x = range(4)
y = [0.35716074449094487, 0.35716078072465335, 0.3571607688695179, 0.35716057632266723]
ax.plot(x, y)
# plt.savefig("test.png")
save("test.tex", standalone=True)

The resulting .tex code is as follows

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\DeclareUnicodeCharacter{2212}{−}
\usepgfplotslibrary{groupplots,dateplot}
\usetikzlibrary{patterns,shapes.arrows}
\pgfplotsset{compat=newest}
\begin{document}
% This file was created by tikzplotlib v0.9.6.
\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.15, xmax=3.15,
xtick style={color=black},
y grid style={white!69.0196078431373!black},
ymin=0.357160566102568, ymax=0.357160790944753,
ytick style={color=black}
]
\addplot [semithick, color0]
table {%
0 0.357160744490945
1 0.357160780724653
2 0.357160768869518
3 0.357160576322667
};
\end{axis}

\end{tikzpicture}

\end{document}

which unfortunately, does not compile. The expected output is something like this:
test

I tried fixing this by doing manual scaling on the resulting .tex file, but the TeX engine is not precise enough. For reference, I tried adding this to the axis arguments.

scaled y ticks=manual:{}{\pgfmathparse{(#1 - 0.35716)*1e7}},

The issue could be fixed by doing the scaling beforehand, but that might be annoying because it seems to be a TeX issue.

This appears to be a pgfplots issue; best post it on https://github.com/pgf-tikz/pgfplots.