TransWikia.com

how to plot multiple data from .dat file?

TeX - LaTeX Asked on October 5, 2021

I am trying to plot a multiple coloum data in latex using pgfplots. I have tried to get the graph but I am unable to plot the graph. My code is as follows and how can I add legends? Please guide me.

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}

One Answer

pgfplotstable assumes whitespace-delimited columns, when you have comma as a column delimiter you need to specify it:

pgfplotstableread[col sep=comma]{mydata.dat}{mydata}

For legends: either

legend{a,b,c}

or multiple instances of addlegendentry, e.g.

addlegendentry{a}
addlegendentry{b}
addlegendentry{c}

I'd also recommend having pgfplotsset{compat=1.3} in the preamble, if you don't have a compat=<some higher version number> already. The obvious benefit is that the ylabel gets a better placement.

output of the code below

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}

Answered by Torbjørn T. on October 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP