# Problems with tikz picture generated by matplotlib2tikz

I am trying to automatically generate a few hundred graphs using pgfplots. To run over the textfiles I have used the following code in python

for fname in os.listdir(cwd):
X=data[:,0]
Y=data[:,1]
plt.plot(X,Y, label=fname)
plt.legend()
tikz_save('test.tikz')
plt.show() #or


Where the textfiles are very simple

0 6.60951137543e-07
1 1.39856338501e-06
2 1.47868315379e-06
3 2.24320014318e-06
4 3.22325309118e-06
5 5.61778704325e-06
6 2.59962168154e-05
7 5.43832211406e-05


These files are then stored in a test.tikz file.

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

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

\begin{axis}[
xmin=-0.35, xmax=7.35,
ymin=-1.24923349159472e-05, ymax=0.000272516976146664,
tick align=outside,
tick pos=left,
x grid style={lightgray!92.026143790849673!black},
y grid style={lightgray!92.026143790849673!black},
legend entries={{benchmark_01_PE_002.txt},{benchmark_01_PE_002_naive_fast.txt},{benchmark_01_PE_002_naive.txt},{benchmark_01_PE_002_fast.txt}},
legend cell align={left},
legend style={at={(0.03,0.97)}, anchor=north west, draw=white!80.0!black}
]
table {%
0 6.60951137543e-07
1 1.39856338501e-06
2 1.47868315379e-06
3 2.24320014318e-06
4 3.22325309118e-06
5 5.61778704325e-06
6 2.59962168154e-05
7 5.43832211406e-05
};
table {%
0 4.62633768717e-07
1 3.73329003652e-06
2 5.28554677963e-06
3 8.36355686188e-06
4 1.5385330092e-05
5 3.0366628683e-05
6 7.69478162207e-05
7 0.00016037694877
};
table {%
0 5.29197057088e-07
1 2.85500208537e-06
2 4.15600617727e-06
3 6.91545724869e-06
4 1.28858009703e-05
5 2.47888177303e-05
6 0.000106253957104
7 0.000259562007462
};
table {%
0 4.72676753998e-07
1 1.34051084518e-06
2 1.5517838796e-06
3 2.52573728561e-06
4 4.2628399531e-06
5 6.96866671244e-06
6 3.04404287211e-05
7 7.20652266561e-05
};
\end{axis}

\end{tikzpicture}


I tried to compile the tikzpicture above using the following simple tex.file

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}

\usepackage{tikz}

\begin{document}

\input{test.tikz}

\end{document}


However I have not for the life of me been able to compile the tikzpicture above. I get an error about a missing \$. This is strange as even if I remove all the labels, the file still does not compile. Without the labels there are no text in the file, and thus getting errors about math mode seems bizzare.

Is there an easier way to compile a bunch of txt files into a pgfplots than what I did above?

• You have several _ in the legend entries, they are only allowed in math mode. – Torbjørn T. Oct 17 '17 at 17:51
• Could you please post a complete python code? From where is the tikz_save function? Also: Are you sure this is the correct place to ask such questions? This seems more about python's matplotlib than about LaTeX. – Skillmon Oct 17 '17 at 18:01
• @Skillmon It's from matplotlib2tikz. Anyways, as I said the only problem is the _ in the legend entries which should be \_. It's certainly not about matplotlib. You could possibly argue that matplotlib2tikz doesn't handle underscores properly, but the maintainer has already said that this is something he wont try to fix: github.com/nschloe/matplotlib2tikz/issues/50 – Torbjørn T. Oct 17 '17 at 18:07

As @TorbjørnT. pointed out, your label names contain _ as the file names you used do. You could use the following python code instead:
for fname in os.listdir(cwd):

• @N3buchadnezzar in the same manner you could also remove the trailing .txt from your files (as those are just unnecessary waste of space, imho). – Skillmon Oct 18 '17 at 6:21