Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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};
share|improve this question

1 Answer 1

up vote 24 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.