[Tex/LaTex] Choosing data columns with Tikz plot

tikz-pgf

I want to plot data from a file using Tikz. I have a data file with 5 columns of data, and I want, for example to plot column 1 and 3 together. How can I make Tikz load the data from the correct columns?

Example of data file:

1,3,6
2,4,5
3,3,7
4,1,2
5,1,1

Current Latex:

\draw[color=blue] plot[smooth,mark=*,mark size=1pt] file {data.dat} node [right] {data};

Best Answer

You should use the \addplot table command from pgfplots. The x index=0 and y index=2 specify which column is to be selected (numbering starts from 0). I have two \addplot commands to plot each column separately.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{filecontents}{datax.dat}
1,3,6
2,4,5
3,3,7
4,1,2
5,1,1
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel={$x$},ylabel={Column Data}]

% Graph column 2 versus column 0
\addplot table[x index=0,y index=2,col sep=comma] {datax.dat};
\addlegendentry{Column 3}% y index+1 since humans count from 1

% Graph column 1 versus column 0    
\addplot table[x index=0,y index=1,col sep=comma] {datax.dat};
\addlegendentry{Column 2}

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here