How to fix the trajectory of Brownian motions which generated by the “rand” function with tikz in beamer frames

I draw some Brownian motions in beamer frame with the help of tikz(see Tom's codes). I want to obtain the following effect:

The Brownian motions is truncated by a positive integer. The panoramas of Brownian motions are showed when the positive integer increases.

I try to use some white rectangles for covering Brownian motions with \onslide in beamer and draw a line below the rectangle. But the trajectory of Brownian motions changes when the line rises. Because Brownian motions will be drawn again when the overlay is generated by \onslide. In every overlay the trajectories are different. Here is my MWE:

\documentclass{beamer}
\useoutertheme{infolines}
\usepackage{tikz}
\begin{document}
\begin{frame}{Brownian motions}
\begin{tikzpicture}[scale=0.6]
\draw[help lines] (0,0) grid (15,10);

\draw[red] (0,0)
\foreach \x in {1,2,...,750}
{ -- ++(0.02,rand*0.2+0.01) };

\draw[blue] (0,0)
\foreach \x in {1,2,...,750}
{ -- ++(0.02,rand*0.2+0.01) };

\draw[green] (0,0)
\foreach \x in {1,2,...,750}
{ -- ++(0.02,rand*0.2+0.01) };

\draw[orange] (0,0)
\foreach \x in {1,2,...,750}
{ -- ++(0.02,rand*0.2+0.01) };

\foreach \i in {1,2,...,7}
{
\onslide<\i>{
\draw[thick,red] (0,\i+3) -- ++(15,0);
\draw[white,fill=white,very thin] (0,\i+3) rectangle (15,11);
\draw[help lines] (0,\i+3) grid (15,10);
}
}

\draw[thick,->,>=stealth] (0,0) -- (16,0) node[right] {$t$};
\draw[thick,->,>=stealth] (0,0) -- (0,11) node[above] {$Y_t$};
\end{tikzpicture}
\end{frame}
\end{document}


See the following screenshot:

So I think if I can fix the trajectories, the effect I want will be obtained easily.

I also try to use the following method:

First, draw the trajectories in another document; then use the trajectories as a single pdf with '\includegraphics'.

But I also want to delete the red, orange and green trajectories and leave the blue one alone in the last overlay. Then fixing the trajectories is also important for deleting the trajectories.

How can I get the effect talked above? I really appreciate your work.

-
If you put \pgfmathsetseed{<some integer>} before the first set of \draw commands, the random number generator will be reset for each slide and so always generate the same paths (Tom mentioned this in his answer). –  Jake Jun 16 '12 at 7:29
@Jake It does work. Thanks very much. In Tom's codes, thers is \pgfmathsetseed. But I do not know what effect this code can generate. Sorry for ingore it. –  XIAO Lishun Jun 16 '12 at 7:56

First a few remarks:

• the \foreach loop assumes a difference of one between elements if only a start and end are connected via dots, so \foreach \x in {1,2,...,100} will yield the same as \foreach \x in {1,...,100}
• you paint white over over your RONI (region of no interest). It is better to \clip the ROI (region of interest)
• using beamer commands in a TikZ-picture did not seem to work propperly, so I used the \only<n> specifivation to show the entire pictures on different slides
• while you and me might get from {1,...,7} that the numbers are supposed to be integers, computers don't neccessarily, thats why I converted the outer for-loop variable (\f) to an interger (\i) via \pgfmathtruncatemacro
• the \scope is used to keep the clipping local

And here's the result:

\documentclass{beamer}
\useoutertheme{infolines}
\usepackage{tikz}
\begin{document}

\begin{frame}{Brownian motions}
\foreach \f in {1,...,7}
{   \pgfmathtruncatemacro{\i}{\f}
\only<\i>{\begin{tikzpicture}[scale=0.55]
\draw[help lines] (0,0) grid (15,10);

\begin{scope}
\clip (0,0) rectangle (15,\i+3);
\draw[red] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
\draw[blue] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
\draw[green] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
\draw[orange] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
\end{scope}

\draw[thick,red] (0,\i+3) -- ++(15,0);
\draw[thick,->,>=stealth] (0,0) -- (16,0) node[right] {$t$};
\draw[thick,->,>=stealth] (0,0) -- (0,11) node[above] {$Y_t$};
\end{tikzpicture}}
}
\end{frame}

\end{document}


For putting certain plots only on some slides, you could use the \ifthenelse{condition}{true path}{false path} command from the xifthen package:

\documentclass{beamer}
\useoutertheme{infolines}
\usepackage{tikz}
\usepackage{xifthen}

\begin{document}

\begin{frame}%{Brownian motions}
\foreach \f in {1,...,7}
{ \pgfmathtruncatemacro{\i}{\f}
\only<\i>{
\begin{tikzpicture}[scale=0.55]
\draw[help lines] (0,0) grid (15,10);
\begin{scope}
\clip (0,0) rectangle (15,\i+3);
\draw[blue] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
\ifthenelse{\i<7}
{   \draw[red] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
\draw[green] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
\draw[orange] (0,0)
\foreach \x in {1,...,750}
{ -- ++(0.02,rand*0.2+0.01) };
}{}
\end{scope}
\draw[thick,red] (0,\i+3) -- ++(15,0);
\draw[thick,->,>=stealth] (0,0) -- (16,0) node[right] {$t$};
\draw[thick,->,>=stealth] (0,0) -- (0,11) node[above] {$Y_t$};
\end{tikzpicture}
}
}
\end{frame}

\end{document}


-
Oh, once again to trouble you. I think I should learn more about tikz. –  XIAO Lishun Jun 17 '12 at 13:27

Jake's and Tom's answers to How to draw Brownian motions in tikz/pgf show how to draw pseudo-random plots with TiKZ and pgfplots and here Jake suggested to use \pgfmathsetsed in order to plot the same random path in different slides. What I propose with next code (just an adaptation from Jake's solution) is to save all paths into a file and read them according your needs. This way it's not necessary to repeat all computations every time you process your document.

Package pgfplotstable provides command \pgfplotstablesave{\loadedtable}{brownian.dat} which saves a table (\loadedtable) into a disc file (brownian.dat).

This brownian.dat file will be created through \pgfplotstablenew the first time you process your document, but with \IfFileExists test help all this time consuming process will be avoided on successive compilations.

As is explained in Is it possible to create overlays in beamer inside pgfplots graphics?, you can use \only<> with \addplot and plot one or more curves on every slide. The problem is that pgfplots computes a new axis environment for each slide, so it's necessary to fix axis dimensions for all of them (ymin=-1.5, ymax=2.5 in this case) otherwise ymin and ymax will be adapted to the particular slide and they will be jumping from one slide to the other.

Plotting colors need also to be fixed if you want to show the same for every path in each slide. \addplot[color] table ... is all you need.

This is the code

\documentclass{beamer}
\usepackage{pgfplots, pgfplotstable}

\IfFileExists{brownian.dat}{}{
\pgfmathsetseed{3}

\pgfplotstablenew[
create on use/brown1/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/brown2/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
create on use/brown3/.style={
create col/expr accum={\pgfmathaccuma + 0.1*rand}{0}
},
columns={brown1,brown2,brown3}
]
{700}
}

\begin{document}

\begin{frame}{Brownian}

{\par\centering
\begin{tikzpicture}
\begin{axis}[
line join=bevel,
no markers,
table/x expr={\coordindex/400},
xmin=0,
enlarge x limits=false,
ymin=-1.5,
ymax=2.5,
]

\draw (axis cs:0.4,2.2) -- ({axis cs:0.4,2.2}-|{rel axis cs:1,0})
(axis cs:1.3,-0.7) -- ({axis cs:1.3,-0.7}-|{rel axis cs:1,0});
\end{axis}

\end{tikzpicture}\par}
\end{frame}

\end{document}


and this is the result

-
Ah, that's a very clever idea! –  Jake Jun 19 '12 at 11:15
Yeah, nice work –  XIAO Lishun Jun 19 '12 at 15:57
@Jake, @xiaodaxialiang: thank you. Some time ago I wanted to plot some signal+noise graphs and already used this idea. Christian Feuersanger showed me the undocumented option plot unprocessed to file which allows to plot to a file instead output so you can forget previous pgfplotstablenew. But Jake's code is very nice. –  Ignasi Jun 20 '12 at 9:49