I am using a foreach loop to add plots to an axis environment and want each plot to have its own color. To achieve this I tried the following:
\foreach \x/\barcolor in {1/red, 2/green, 3/blue, 4/gray}{
\addplot[fill=\barcolor] table[x=key,y expr={\thisrowno{\x}/\thisrowno{6}}] {data.dat};
}
But somehow there is a problem with the \barcolor
variable. If I use fill=blue
the loop runs perfectly fine, but obviously all bars are blue. Unfolding the loop and manually adding the four plot is working fine too:
\addplot[fill=red] table[x=key,y expr={\thisrowno{1}/\thisrowno{6}}] {data.dat};
\addplot[fill=green] table[x=key,y expr={\thisrowno{2}/\thisrowno{6}}] {data.dat};
\addplot[fill=blue] table[x=key,y expr={\thisrowno{3}/\thisrowno{6}}] {data.dat};
\addplot[fill=gray] table[x=key,y expr={\thisrowno{4}/\thisrowno{6}}] {data.dat};
I cannot figure the problem with my loop and hope you can help me.
\begingroup\edef\x{\endgroup\noexpand\addplot[fill=\barcolor]}\x
instead of\addplot[fill=\barcolor]
\addplot[fill/.expanded=\barcolor]