4

I want to plot a series of functions, each in another color. I would like to do this with a foreach, but it doesn't work. First I try a simple tikzpicture (from another mwe), this works. Then the elaborated pgfplots with 5 different addplots, this works. But the third tikzpicture gives me errors. Can anyone help me?

tia Guido

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{nicefrac}

\begin{document}
\begin{tikzpicture}
  \foreach \x/\y in {1/blue, 2/red, 3/blue, 4/red}
    \fill[color=\y] (\x,0) rectangle +(1,1);
\end{tikzpicture}

\def\U{sqrt(8)}
\def\X{4}

\begin{tikzpicture}[font=\small]
\begin{axis}[
title=$T-n\;characteristic$,
domain=0:1,
xmin=-0.01,
xmax=1.1,
ymin=-0.01,
ymax=1.1,
axis lines=middle,
grid,
xlabel style={anchor=north},
xlabel={$\nicefrac{n}{{n_s}}$},
ylabel style={anchor=north east},
ylabel={$\nicefrac{T}{T_k}$},
samples=100]
  \def\R{0.3}
    \addplot[no marks,blue]{2*(\R/\X)*(1-x)/((1-x)^2+(\R/\X)^2)};
  \def\R{0.6}
    \addplot[no marks,orange]{((2*\R*\X)*(1-x))/((\X*(1-x))^2+\R^2)};
  \def\R{1}
    \addplot[no marks,green]{2*(\R/\X)*(1-x)/((1-x)^2+(\R/\X)^2)};
  \def\R{2}
    \addplot[no marks,yellow]{2*(\R/\X)*(1-x)/((1-x)^2+(\R/\X)^2)};
  \def\R{3}
    \addplot[no marks,brown]{2*(\R/\X)*(1-x)/((1-x)^2+(\R/\X)^2)};
  \def\R{4}
    \addplot[no marks,red]{2*(\R/\X)*(1-x)/((1-x)^2+(\R/\X)^2)};
  \def\R{5}
    \addplot[no marks,purple]{2*(\R/\X)*(1-x)/((1-x)^2+(\R/\X)^2)};
\end{axis}
\end{tikzpicture}

\def\U{sqrt(8)}
\def\X{4}

\begin{tikzpicture}[font=\small]
\begin{axis}[
title=$T-n characteristic$,
domain=0:1,
xmin=-0.01,
xmax=1.1,
ymin=-0.01,
ymax=1.1,
axis lines=middle,
grid,
xlabel style={anchor=north},
xlabel={$\nicefrac{n}{{n_s}}$},
ylabel style={anchor=north east},
ylabel={$\nicefrac{I}{I_m}$},
samples=100]
begin{scope}
\foreach \R/\kl in {1/blue, 2/orange, 3/green, 4/yellow, 5/brown}
%  \fill[color=\kl] (\R,0) rectangle +(1,1);
      \addplot[no marks,color=\kl]{(\U*\U*\R*(1-\x))/((\X*(1-\x))^2+\R^2)};
end{scope}
\end{axis}
\end{tikzpicture}
\end{document}
Guido VDV
  • 121

1 Answers1

7

The standard \foreach does not work in pgfplots in the usual way. Therefore there is a command \pgfplotsinvokeforeach. However, if you force expansion, you can use standard \foreach as well, a trick I learned from the author of pgfplots himself.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{nicefrac}

\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[
title=$T-n characteristic$,
domain=0:1,
xmin=-0.01,
xmax=1.1,
ymin=-0.01,
ymax=1.1,
axis lines=middle,
grid,
xlabel style={anchor=north},
xlabel={$\nicefrac{n}{{n_s}}$},
ylabel style={anchor=north east},
ylabel={$\nicefrac{I}{I_m}$},
samples=100]
\begin{scope}
\pgfmathsetmacro\U{sqrt(8)}
\pgfmathsetmacro\X{4}

\foreach \R/\kl in {1/blue, 2/orange, 3/green, 4/yellow, 5/brown}
{      \edef\temp{\noexpand\addplot[no
      marks,color=\kl]{(\U*\U*\R*(1-x))/((\X*(1-x))^2+\R^2)};}
      \temp}
\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

For completeness and John Kormylo: the same result with \pgfplotsinvokeforeach. Advantage: no \noexpand hackery, disadvantage: does not support two or more loop variables so one needs to introduce lists and/or counters.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{nicefrac}
\newcounter{ForJohn} %;-)
\begin{document}
\begin{tikzpicture}[font=\small]
\begin{axis}[
title=$T-n characteristic$,
domain=0:1,
xmin=-0.01,
xmax=1.1,
ymin=-0.01,
ymax=1.1,
axis lines=middle,
grid,
xlabel style={anchor=north},
xlabel={$\nicefrac{n}{{n_s}}$},
ylabel style={anchor=north east},
ylabel={$\nicefrac{I}{I_m}$},
samples=100]
\begin{scope}
\pgfmathsetmacro\U{sqrt(8)}
\pgfmathsetmacro\X{4}

\pgfplotsinvokeforeach{blue,orange,green,yellow,brown}{\stepcounter{ForJohn}      
\addplot[no marks,color=#1]{(\U*\U*\number\value{ForJohn}*(1-x))/((\X*(1-x))^2+pow(\number\value{ForJohn},2))};}

\end{scope}
\end{axis}
\end{tikzpicture}
\end{document}