11

The following MWE works nicely when I remove the optional argument [\forcol] from the \addplotcommand or when I change it to red, for example. Though I would like the curves to have different colors as defined in my list {1/black,2/red,3/green,4/blue,5/orange}, so I tried it as the following, but then it throws me the error

"! Undefined control sequence. \end{axis}"

\documentclass{article} \usepackage{xcolor} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \foreach \foridx/\forcol in {1/black,2/red,3/green,4/blue,5/orange}{ \addplot[\forcol]{x^\foridx}; } \end{axis} \end{tikzpicture} \end{document} 

What is my mistake? I would like to have this feature to set up a command for plotting many data files.

3
  • 3
    referencing section 8 of the manual, you can use \pgfplotsforeachungrouped \foridx/\forcol in {1/black,2/red,3/green,4/blue,5/orange}{ \addplot[\forcol]{x^\foridx}; \edef\temp{\noexpand\addplot[\forcol]{x^\foridx};} \temp } Commented Mar 28, 2016 at 14:29
  • 1
    @cmhughes: great, this works, thanks! You can create an answer of this! But I don't really understand this "magic" (even with the description of the manual), what the "extra groups" are that are not introduced here. Commented Mar 28, 2016 at 14:37
  • @cmhughes: I just realize you had the ` \addplot[\forcol]{x^\foridx};` also outside your temp environment. This should be omitted. Commented Mar 28, 2016 at 17:11

1 Answer 1

10

You can try some \expandafter... statements to expand the \foreach loops. \foreach loops can be quite tricky, in my point of view, due to the way the \forcol macros are defined by pgffor.

\documentclass{article} \usepackage{xcolor} \usepackage{pgfplots} \begin{document} \begin{tikzpicture} \begin{axis} \pgfplotsforeachungrouped \foridx/\forcol in {1/black,2/red,3/green,4/blue,5/orange}{ \expandafter\addplot\expandafter[\forcol]{x^\foridx}; } \end{axis} \end{tikzpicture} \end{document} 

enter image description here

13
  • i simply removed [\forcol] and the plot showed. but it has a lot of points in it. Commented Mar 28, 2016 at 14:40
  • 1
    @riddleculous: It can be really complicated with \expandafters, in my point of view, in nested \foreach loops and multiple \addplot[\foo,\forbar,\morefoo]{...} Commented Mar 28, 2016 at 14:53
  • 3
    Your and cmhughes code are not the same. You are both expanding the color command (which is needed), but cmhughes also avoid the grouping around the \addplots. I would suggest to use \pgfplotsforeachungrouped in your code too. Commented Mar 28, 2016 at 14:53
  • 1
    @UlrikeFischer: Thanks, I've replaced \pgfplotsforeachungrouped instead of the usual \foreach Commented Mar 28, 2016 at 15:00
  • 1
    @riddleculous: The axis environment defers a lot work to the end (e.g. the generation of the legend), and the addplots are not independant (see e.g. the cycle list, labels, maximum etc), so I would avoid to separate them. Commented Mar 28, 2016 at 15:07

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.