PGFplotstable: using a csv file with 4 columns

Suppose I have a csv file with no titles in it just data. The first column is time (x axis) and the next 3 columns each construct a different set; that is, plot(t, col1) is one set of data, plot(t, col2), etc.

I can load the csv with the following command

\pgfplotstableread[col sep = comma]{data.csv}\mydata


How would I then plot each line?

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend pos = south east,
xmin = 0,
xmax = 20,
ymin = 0,
ymax = 2.5
]
\addplot table[x = time, y = 2nd col]{\mydata};
\addplot table[x = time, y = 3rd col]{\mydata};
\addplot table[x = time, y = 4th col]{\mydata};
\addplot table[x = time, y = 5th col]{\mydata};
\legend{Actual, Measured, Est $Q = 0.01$, Est $Q = 0.1$}
\end{axis}
\end{tikzpicture}
\end{document}


Does pgfplots have an internal label name for each column? Can I change it to time, actual, measured, Q1, Q2?

In the code above, I used x = time and y = ith col to show what I want where. Those aren't correct and it wont compile since I don't know how to distinguish the columns in the csv file with pgfplots.

• Would this help? tex.stackexchange.com/q/103179/34618 – Jesse May 5 '14 at 1:54
• Do the x index={0}, y index={1}... options not work for you? Also, if the x index will be the same for each plot, you can set it in the axis options by adding table/x index={0}, (I'm not sure if the table/ is necessary, but I think it might be...) – darthbith May 5 '14 at 2:06
• @darthbith I am not saying something doesn't work. I don't know how to change it. So for the first plot data, I would have x = {0}, y = {1}? – dustin May 5 '14 at 2:23
• No, x index={0}, y index={0}. I wasn't sure if you had seen that option in the manual and weren't able to use it for some reason :-) In the pgfplots manual, version 1.10, its on page 49 – darthbith May 5 '14 at 2:25
• @darthbith I was looking through the pgfplotstable manual – dustin May 5 '14 at 2:27

If you do not have names for the columns in your csv file (i.e. in a header row) you can use the column indices, starting with zero to refer to them. See page 49 of version 1.10 of the pgfplots manual

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend pos = south east,
xmin = 0, xmax = 20,
ymin = 0, ymax = 2.5,
]
\addplot table[x index = {0}, y index = {1}]{\mydata};
\legend{Actual}
\end{axis}
\end{tikzpicture}
\end{document}


If you'll be plotting multiple things with common options, you can specify the options in the axis options, like specifying the x index:

...
\begin{axis}[
legend pos = south east,
xmin = 0, xmax = 20,
ymin = 0, ymax = 2.5,
table/x index={0},
]

If you have any letter characters on the first row, you will also have to specify header = false, either when you load the table or in the axis options. This includes if you have nan in the first row.