![如何从.dat 文件绘制多个数据?](https://linux22.com/image/418469/%E5%A6%82%E4%BD%95%E4%BB%8E.dat%20%E6%96%87%E4%BB%B6%E7%BB%98%E5%88%B6%E5%A4%9A%E4%B8%AA%E6%95%B0%E6%8D%AE%EF%BC%9F.png)
我正在尝试使用 pgfplots 在 latex 中绘制多列数据。我尝试获取图表,但无法绘制图表。我的代码如下,我该如何添加图例?请指导我。
\documentclass[]{article}
\usepackage{filecontents,pgfplots}
\begin{filecontents}{pistonkinetics.dat}
m0_FEED, AT 773.15K, AT 823.15K, AT 873.15K, AT 923.15K, AT 973.15K
0.005, 1.30E+04, 12959.71536,12959.71536,12959.71536,12959.71536
0.007, 1.81E+04, 18145.78478,18145.78478,18145.78478,18145.784
0.009, 2.33E+04, 23333.9541,23333.9541,23333.9541,23333.9541
0.011, 2.85E+04, 28524.71381,28524.71381,28524.71381,28524.71381
0.013, 3.37E+04, 33718.48576,33718.48576,33718.48576,33718.48576
0.015, 3.89E+04, 38915.61947,38915.61947,38915.61947,38915.61947
0.017, 4.41E+04, 44116.39602,44116.39603,44116.39603,44116.39603
0.019, 4.93E+04, 49321.02848,49321.02848,49321.02878,49321.02849
0.021, 5.45E+04, 54529.67237,54529.67238,54529.67238,54529.67238
\end{filecontents}
\begin{document}
\pgfplotstableread{mydata.dat}{\mydata}
\begin{tikzpicture}[scale=1]
\begin{axis}[minor tick num=1,
xlabel=Degrees, ylabel = temperature]
\addplot [black,very thick] table [x={m0_FEED}, y={AT 773.15K}] {\mydata};
\legend{AT773.15K}
\addplot [red,very thick] table [x={m0_FEED}, y={AT 823.15K}] {\mydata};
\legend{AT773.15K}
\addplot [blue,very thick] table [x={m0_FEED}, y={AT 873.15K}] {\mydata};
\legend{AT873.15K}
\addplot [green,very thick] table [x={m0_FEED}, y={AT 923.15K}] {\mydata};
\legend{AT923.15K}
\addplot [dashed,yellow,very thick] table [x={m0_FEED}, y={AT 973.15K}] {\mydata};
\legend{AT973.15K}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
pgfplotstable
假设列以空格分隔,当您使用逗号作为列分隔符时,您需要指定它:
\pgfplotstableread[col sep=comma]{mydata.dat}{\mydata}
对于传奇来说:
\legend{a,b,c}
或多个实例\addlegendentry
,例如
\addlegendentry{a}
\addlegendentry{b}
\addlegendentry{c}
如果您还没有,我还建议您将其放在\pgfplotsset{compat=1.3}
序言中compat=<some higher version number>
。 明显的好处是 ylabel 可以得到更好的位置。
\documentclass[border=5mm]{standalone}
\usepackage{filecontents,pgfplots}
\pgfplotsset{compat=1.3}
\begin{filecontents}{mydata.dat}
m0_FEED, AT 773.15K, AT 823.15K, AT 873.15K, AT 923.15K, AT 973.15K
0.005, 1.30E+04, 12959.71536,12959.71536,12959.71536,12959.71536
0.007, 1.81E+04, 18145.78478,18145.78478,18145.78478,18145.784
0.009, 2.33E+04, 23333.9541,23333.9541,23333.9541,23333.9541
0.011, 2.85E+04, 28524.71381,28524.71381,28524.71381,28524.71381
0.013, 3.37E+04, 33718.48576,33718.48576,33718.48576,33718.48576
0.015, 3.89E+04, 38915.61947,38915.61947,38915.61947,38915.61947
0.017, 4.41E+04, 44116.39602,44116.39603,44116.39603,44116.39603
0.019, 4.93E+04, 49321.02848,49321.02848,49321.02878,49321.02849
0.021, 5.45E+04, 54529.67237,54529.67238,54529.67238,54529.67238
\end{filecontents}
\begin{document}
\pgfplotstableread[col sep=comma]{mydata.dat}{\mydata}
\begin{tikzpicture}
\begin{axis}[
minor tick num=1,
xlabel=Degrees,
ylabel = temperature
]
\addplot [black,very thick] table [x={m0_FEED}, y={AT 773.15K}] {\mydata};
\addlegendentry{AT773.15K}
\addplot [red,very thick] table [x={m0_FEED}, y={AT 823.15K}] {\mydata};
\addlegendentry{AT823.15K}
\addplot [blue,very thick] table [x={m0_FEED}, y={AT 873.15K}] {\mydata};
\addlegendentry{AT873.15K}
\addplot [green,very thick] table [x={m0_FEED}, y={AT 923.15K}] {\mydata};
\addlegendentry{AT923.15K}
\addplot [dashed,yellow,very thick] table [x={m0_FEED}, y={AT 973.15K}] {\mydata};
\addlegendentry{AT973.15K}
% for the legend entries you can alternatively use
% \legend{AT773.15K, AT823.15K, AT873.15K, AT923.15K, AT973.15K}
\end{axis}
\end{tikzpicture}
\end{document}