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 have a table (10000 rows and 14 cols) in a ascii file, which contain only the y - components.

In my case the x's values are 1 till 10000.

Is it possible to tell Tikz to plot from a table only with the y-component? I am aware of x expr=\coordindex, but then i need to import each column separately using addplot (in my case 14 times) and i need to define the color of each column separately.

Is there an elegant solution to read the table, use the position of the y value as x position and different color for each column?

share|improve this question
add comment

1 Answer

You can extract the number of columns using \pgfplotsgetcolsof{<table macro or file name>}, and then loop over the columns using \pgfplotsinvokeforeach (which is similar to TikZ' \foreach):

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\pgfplotstableread{
Y1  Y2  Y3  Y4
1   2   2   5
1   3   7   1
8   2   3   1
9   5   3   6
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[ymin=0]
\pgfplotstablegetcolsof{\datatable}
\pgfmathsetmacro\numberofycols{\pgfplotsretval-1}
\pgfplotsinvokeforeach {0,...,\numberofycols}{
    \addplot table [x expr=\coordindex, y index=#1] {\datatable};
}
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
how would this solution be change if i read the table from a file? –  Eagle Feb 15 '12 at 17:01
    
i would like to generate legend as well. How could i do it? –  Eagle Feb 15 '12 at 17:31
    
@Eagle: Just change the pgfplotstableread line to read from your file. As for the legend part, that's a separate question I think. Try some \addlegendentrys as the documentation explains. –  sappjw May 29 '12 at 13:23
add comment

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.