3

I have a table with 8 columns, and I want to generate 4 plots (1 vs 0, 3 vs 2, 5 vs 4, and 7 vs 6). I was going to use foreach to generate the plots using

\begin{tikzpicture}
\begin{axis}
\foreach \x in {0,...,3} {
\addplot table[x index = \x * 2, y index = \x * 2 + 1]\table
}
\end{axis}
\end{tikzpicture}

where \table is a stored table.

But I don't seem to permitted to do arithmetic on the indices. Is there a way to do this ?

Update: not-quite-m WE:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\begin{tikzpicture}
\pgfplotstableread{
-1.5875 -1.5324 -1.4773 -1.4222 -1.3671 -1.312  -1.2569 -1.2018
-1.5875 -1.5324 -1.4773 -1.4222 -1.3671 -1.312  -1.2569 -1.2018
0.013166    0.013156    0.013147    0.013141    0.013137    0.013135    0.013135    0.013139
0.013156    0.013146    0.013138    0.013132    0.013128    0.013127    0.013128    0.013131
}\loadedtable
\pgfplotstabletranspose[colnames from=,input colnames to=r]\transtable\loadedtable
  \begin{axis}
\foreach \x in {0,...,1} {
  \pgfmathtruncatemacro{\Xcol}{\x * 2}
  \pgfmathtruncatemacro{\Ycol}{1 + \x * 2}
  \addplot[mark=,color=blue] table[x index =\Xcol,y index =\Ycol]\transtable;
}
  \end{axis}
\end{tikzpicture}
\end{document}
  • I can't test it since you did not provide a MWE, but you could try \pgfmathtruncatemacro{\XColumn}{\x*2}, and then use x index = XColumn. Similarly for the y index. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Dec 5 '12 at 2:58
  • I completely understand. it's just that my data source is a transposed table (hence the not-quite-minimal working example) and it took me a while to extract a meaningful snippet for testing. The above should be testable. – Suresh Dec 5 '12 at 3:13
6

For this case I would just use simple number expressions otherwise you have to use \pgfmathparse{...}\pgfmathresult pairs and it would be tedious. Instead do the arithmetic using evaluate key and use the resulting macro. There are a few examples in the PGF/TikZ manual :

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.7}
\pgfplotstableread[header=false]{
0 1 2 3 4 5 6 7
5 8 6 4 0 6 7 8
}\mytable
\begin{document}
\begin{tikzpicture}
\begin{axis}
\foreach \x in {0,...,3} {
\addplot table[x index=\number\numexpr\x*2\relax,
y index=\number\numexpr\x*2+1\relax] {\mytable};
}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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