我正在尝试使用 pgfplots 自动生成几百张图表。为了在文本文件上运行,我在 python 中使用了以下代码
for fname in os.listdir(cwd):
data=np.loadtxt(cwd + '/' + fname)
X=data[:,0]
Y=data[:,1]
plt.plot(X,Y, label=fname)
plt.legend()
tikz_save('test.tikz')
plt.show() #or
文本文件非常简单
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
然后将这些文件存储在一个test.tikz
文件中。
% 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}
]
\addlegendimage{no markers, color0}
\addlegendimage{no markers, color1}
\addlegendimage{no markers, color2}
\addlegendimage{no markers, color3}
\addplot [semithick, color0]
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
};
\addplot [semithick, color1]
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
};
\addplot [semithick, color2]
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
};
\addplot [semithick, color3]
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}
我尝试使用以下简单的 tex.file 编译上面的 tikzpicture
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{tikz}
\begin{document}
\input{test.tikz}
\end{document}
但是我终其一生都没能编译上面的 tikzpicture。我收到一个关于缺少 $ 的错误。这很奇怪,因为即使我删除了所有标签,文件仍然无法编译。没有标签,文件中就没有文本,因此收到有关数学模式的错误似乎很奇怪。
有没有比我上面所做的更简单的方法将一堆 txt 文件编译成 pgfplots?
答案1
正如@TorbjørnT. 指出的那样,您的标签名称_
与您使用的文件名一样。您可以改用以下 python 代码:
for fname in os.listdir(cwd):
data=np.loadtxt(cwd + '/' + fname)
X=data[:,0]
Y=data[:,1]
plt.plot(X,Y, label=fname.replace('_',' '))
plt.legend()
tikz_save('test.tikz')
plt.show() #or