2

I have found the following solution in stackexchange. It has been perfectly answered here by @Jake.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    set layers=standard,
    domain=0:10,
    samples y=1,
    view={40}{20},
    hide axis,
    unit vector ratio*=1 2 1,
    xtick=\empty, ytick=\empty, ztick=\empty,
    clip=false
]
\def\sumcurve{0}
\pgfplotsinvokeforeach{0.5,1.5,...,5.5}{
    \draw [on layer=background, gray!20] (axis cs:0,#1,0) -- (axis cs:10,#1,0);
    \addplot3 [on layer=main, blue!30, smooth, samples=101] (x,#1,{sin(#1*x*157))/(#1*2)});

    \addplot3 [on layer=axis foreground, very thick, blue,ycomb, samples=2] (10.5,#1,{1/(#1*2)});
    \xdef\sumcurve{\sumcurve + sin(#1*x*(157))/(#1*2)}
}
\addplot3 [red, samples=200] (x,0,{\sumcurve});

\draw [on layer=axis foreground]  (axis cs:0,0,0) -- (axis cs:10,0,0);
\draw (axis cs:10.5,0.25,0) -- (axis cs:10.5,5.5,0);
\end{axis}
\end{tikzpicture}
\end{document}

I really liked the solution. I would be thankful if someone could explain the following features that have been utilized in the code so that I learn it and use it in the future.

  • \pgfplotsinvokeforeach: I read the manual and I still do not see its difference with \foreach. Why we are not using foreach?
  • \addplot3 (x,#1,{sin(#1*x*(157))/(#1*2)});: what is the difference between x and #1 in this line code? What values x will take?
  • \def\sumcurve{0}: Does this one defines y=0 curve?
  • I am a little bit confused about the layers in the code. Why do we need the layers?
  • unit vector ratio*=1 2 1: Is this used to simulate a nice three dimensional graph as shown here?

1 Answers1

5

\pgfplotsinvokeforeach is different from \foreach. To begin with \foreach introduces scoping, \pgfplotsinvokeforeach does not. Compare:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\def\myvalueA{0}
\def\myvalueB{0}
\begin{document}
\begin{tikzpicture}
  \node[anchor=east] at (0,0) {foreach:};
  \foreach \myn in {1,2,3,4,5}
  { 
    \edef\myvalueA{\number\numexpr\myvalueA+\myn} 
    \node at (\myn,0) {\myvalueA};
  }
  \node at (6,0) {\myvalueA};

 \node[anchor=east] at (0,2) {pgfplotsinvokeforeach:};
 \pgfplotsinvokeforeach{1,2,3,4,5}
  {
    \edef\myvalueB{\number\numexpr\myvalueB+#1\relax}
    \node at (#1,2) {\myvalueB};
  }
  \node at (6,2) {\myvalueB};
\end{tikzpicture}
\end{document}

enter image description here

Because of the scoping introduced by \foreach, the new value of \myvalueA is lost after each iteration. When the \foreach loop is completed, \myvalueA resumes the value it was initialized to. In order to get around this with a \foreach loop, you would have to use \xdef instead of \edef.

\pgfplotsinvokeforeach does not introduce such scoping. So, the new value of \myvalueB is remembered between interations.

With \addplot3 (x,#1,{sin(#1*x*(157))/(#1*2)});, x ranges over the domain defined within the axis declaration, #1 ranges over the values passed to \pgfplotsinvokeforeach in its first argument.

\def\sumcurve{0} merely initializes the \sumcurve to that in the first iteration

 \xdef\sumcurve{\sumcurve + sin(#1*x*(157))/(#1*2)}

the \sumcurve in the definition has a value to be added to. Otherwise in the first iteration, \sumcurve would be an undefined control sequence.

If you compile without layers then the axes will appear visually on top of the graphs: that's most likely not what you want, particularly to maintain the visual effect of 3-d.

unit vector ratio*=1 2 1 merely creates a nice scale for the axes.

A.Ellett
  • 50,533
  • 1
    @A2009, each curve on the graph could be labeled y=f_i(x), initialize at y=f_0(x) where f_0(x)=0. Then \sumcurve is essentially y=f_0(x)+f_1(x)+f_2(x)+...+f_k(x). If you initialize \sumcurve to 1, that's essentially the same thing as setting f_0(x)=1. – A.Ellett Jun 13 '14 at 00:35
  • Could you also please elaborate on the three layers that have been used, namely, standard, main, and axis foreground? – shashashamti2008 Jun 13 '14 at 01:57
  • 1
    @A2009 They are merely standard names for configuring layers. If you look at the documentation in pgfplots it explains under /pgfplots/layers/standard how these layers are applied. – A.Ellett Jun 13 '14 at 02:21