14

I'm trying to use pgfplots with a .dat file to plot multiple data points on a graph, but for some reason it's plotting the same two columns over and over again, what have I done wrong?. The .dat file I'm using looks like this:

eang    disppos dispvel dispacc 
0.0000  50.0000 0.0000  -32.8125
1.0000  49.9950 -0.5726 -32.8043
2.0000  49.9800 -1.1450 -32.7796
3.0000  49.9550 -1.7168 -32.7386
4.0000  49.9201 -2.2877 -32.6811
5.0000  49.8752 -2.8575 -32.6073
6.0000  49.8204 -3.4258 -32.5172
7.0000  49.7556 -3.9924 -32.4108
8.0000  49.6810 -4.5571 -32.2882
9.0000  49.5966 -5.1194 -32.1495
10.0000 49.5023 -5.6792 -31.9948  
11.0000 49.3983 -6.2361 -31.8241
12.0000 49.2847 -6.7900 -31.6376
13.0000 49.1613 -7.3404 -31.4354
14.0000 49.0284 -7.8872 -31.2176
15.0000 48.8860 -8.4300 -30.9844
16.0000 48.7342 -8.9687 -30.7358
17.0000 48.5730 -9.5028 -30.4721

\pgfplotstableread{pistonkinetics.dat}{\pistonkinetics}
\begin{tikzpicture}[scale=1]
\begin{axis}[minor tick num=1,
xlabel=Degrees]
\addplot [x={eang}, y={disppos},black,very thick] table {\pistonkinetics};
\addplot [x={eang}, y={dispvel},dashed,red,very thick] table {\pistonkinetics};
\addplot [x={eang}, y={dispacc},dashed,blue,very thick] table {\pistonkinetics};
\end{axis}
\end{tikzpicture}
percusse
  • 157,807
Andy
  • 1,363

1 Answers1

19

The x and y options need to be supplied to the table part of the \addplot command, not to \addplot itself:

\documentclass[]{article}
\usepackage{filecontents,pgfplots}

\begin{filecontents}{pistonkinetics.dat}
eang    disppos dispvel dispacc 
0.0000  50.0000 0.0000  -32.8125
1.0000  49.9950 -0.5726 -32.8043
2.0000  49.9800 -1.1450 -32.7796
3.0000  49.9550 -1.7168 -32.7386
4.0000  49.9201 -2.2877 -32.6811
5.0000  49.8752 -2.8575 -32.6073
6.0000  49.8204 -3.4258 -32.5172
7.0000  49.7556 -3.9924 -32.4108
8.0000  49.6810 -4.5571 -32.2882
9.0000  49.5966 -5.1194 -32.1495
10.0000 49.5023 -5.6792 -31.9948  
11.0000 49.3983 -6.2361 -31.8241
12.0000 49.2847 -6.7900 -31.6376
13.0000 49.1613 -7.3404 -31.4354
14.0000 49.0284 -7.8872 -31.2176
15.0000 48.8860 -8.4300 -30.9844
16.0000 48.7342 -8.9687 -30.7358
17.0000 48.5730 -9.5028 -30.4721
\end{filecontents}

\begin{document}
\pgfplotstableread{pistonkinetics.dat}{\pistonkinetics}
\begin{tikzpicture}[scale=1]
\begin{axis}[minor tick num=1,
xlabel=Degrees]
\addplot [black,very thick] table [x={eang}, y={disppos}] {\pistonkinetics};
\addplot [dashed,red,very thick] table [x={eang}, y={dispvel}] {\pistonkinetics};
\addplot [dashed,blue,very thick] table [x={eang}, y={dispacc}] {\pistonkinetics};
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • 1
    That's great! You can mark this question as answered by clicking the green check mark next to the answer. – Jake Feb 15 '12 at 12:51
  • If you have a function, say sin(x), is there any way to put in an x axis at y=0 whilst retaining the box layout? I've tried [axis x line=center], but this gets rid of the box around the outside and brings the tick labels in, I just want the line. – Andy Feb 15 '12 at 13:16
  • You can add extra description/.append code={\draw ({axis cs:0,0}-|{rel axis cs:0,0}) -- ({axis cs:0,0}-|{rel axis cs:1,0});} to your axis options to draw the 0 line. – Jake Feb 15 '12 at 13:26
  • @jake is there an easy way to create a dat file of this format from an excel sheet? saving it as csv means separated by commas and manually spacing it is a nightmare for large number of entries. – Sndn Dec 05 '17 at 14:05