4

I am trying to loop through a parameter to produce shifted graphs in grouppplots, as in the following MWE, however the value of \k doesn't seem to be recognized in the argument of sin, i.e. the black lines are not shifted. I inserted a red line with shift 0.25 manually for comparison. What did I do wrong?

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}

\begin{tikzpicture}
\begin{groupplot}[group style={group size=1 by 3},width=\textwidth,height=4cm,xmin=0,xmax=2,samples=200]
\pgfplotsforeachungrouped \k in {0,0.25,0.5}{
  \nextgroupplot
  \addplot[domain=0:2] gnuplot {sin(2*pi*(x - \k))} node[above,pos=0.5]{\k};
  \addplot[domain=0:2,red,dashed] gnuplot {sin(2*pi*(x - 0.25))} node[above,pos=0.5]{\k};
}
\end{groupplot}
\end{tikzpicture}

\end{document}

Output:

enter image description here

2

Welcome to the expanding universe! (Yes, these expansion tricks can easily drive one close to madness. ;-))

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\begin{document}

\begin{tikzpicture}
\begin{groupplot}[group style={group size=1 by 3},width=\textwidth,height=4cm,xmin=0,xmax=2,samples=200]
\pgfplotsinvokeforeach{0,0.25,0.5}{
  \nextgroupplot
  \edef\temp{\noexpand\addplot[domain=0:2] gnuplot {sin(2*pi*(x - #1))}
  node[above,pos=0.5]{#1};}
  \temp
  \edef\temp{\noexpand\addplot[domain=0:2,red,dashed] gnuplot {sin(2*pi*(x -
  0.25))} node[above,pos=0.5]{#1};}
  \temp
}
\end{groupplot}
\end{tikzpicture}
\end{document}

enter image description here

"EXPLANATION": When playing with pgfplots for a while, one comes across some standard tricks which one applies until one gets the desired result. ;-)

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.