5

I am trying to plot 12 different lines with a for loop and with a gradually changing color. I came up with this simple code:

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    ylabel={Normalized Intensity (a.u.)},
    xlabel={Energy (eV)},
    ]
        \foreach \column in {1,...,12}{
            \addplot[style = solid, color = red!\column!black] table[x index = {0}, y index = {\column}] {../Data/12spectra.asc};
        }
\end{axis}
\end{tikzpicture}
\caption{blablablabla}
\end{figure}

but the command red!\column!black does not work. I thought that the keyword \column would have been substituted at each occurrence with its value... The error I get is

!Undefined control sequence.
\GenericError ...
#4 \errhelp \@err@ ...
l.75 \end{axis}

I have seen this question Changing color in foreach, but it didn't help me.

I have included only the pgfplots package, do I need another one?

Do you have any suggestion on how to fix the problem?

gabelach
  • 83
  • 1
  • 5

1 Answers1

3

Use instead pgfplotsinvokeforeach such that the value is expanded

\begin{tikzpicture}
\begin{axis}[
    ylabel={Normalized Intensity (a.u.)},
    xlabel={Energy (eV)},
    ]
        \pgfplotsinvokeforeach{1,...,12}{
            \addplot[style = solid, color = red!#1!black] table[x index = {0}, y index = {#1}] {../Data/12spectra.asc};
        }
\end{axis}
\end{tikzpicture}

Also don't use names that are very likely to be keywords in some packages such as \column.

percusse
  • 157,807
  • 1
    Thanks a lot! This works very well! If I wanted to change the color range, could I use something like \pgfmathparse{#1*8} and \pgfmathresult? – gabelach Mar 06 '16 at 19:24
  • edit: I solved with using color = {rgb,12:red,#1;green,1;blue,1}; Thanks again for the answer! – gabelach Mar 06 '16 at 19:31