[Tex/LaTex] PGFplotstable: using a csv file with 4 columns

csvpgfplotspgfplotstable

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}
\pgfplotstableread[col sep = comma]{fexam1cdata.csv}\mydata
\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.

Best Answer

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}
\pgfplotstableread[col sep = comma]{fexam1cdata.csv}\mydata
\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},
    ]
    \addplot table[y index={1}]{\mydata};
...

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.