# How to repeatedly show variations on a plot

I would like to show graphics repeatedly with changes in parameters. Neither of the following show anything. Nor do they give an error message.

Do[Plot[y = Sqrt[(1/n) x], {x, 0, 1}], {n, 1, 5}]

For[n = 1, n < 6, n++, Plot[y = Sqrt[(1/n) x], {x, 0, 1}]]


These merely show the plot for n=1.

Do[Return[Plot[y = Sqrt[(1/n) x], {x, 0, 1}]], {n, 1, 5}]

Do[(Pause[1]; Return[Plot[y = Sqrt[(1/n) x], {x, 0, 1}]]), {n, 1, 5}]

For[n = 1, n < 6, n++, Return[Plot[y = Sqrt[(1/n) x], {x, 0, 1}]]]


(I tried Pauses in the others, to no avail.)

How do I make this work?

• In addition to the great answers below I would also recommend taking a look at this URL. All those free apps are made with Mathematica and you can download the source code and see how it works. demonstrations.wolfram.com/search.html?query=parabola Feb 21, 2012 at 2:03

You probably want Animate, if your intent is to show the plot varying for different n. Here's an example:

Animate[Plot[Sqrt[(1/n) x], {x, 0, 1}, PlotRange -> {All, {0, 1}}], {n, 1, 5}]


What you use depends on what you are looking for.

But, first things first, neither Do nor For will work, as you found out, as both explicitly only return a value (a plot is considered a value) if Return is used. (See below for ways to get For and Do to work.) Unfortunately, Return breaks out of the For and Do immediately, so only the n==1 iteration is evaluated. So, it pays to use a function that is built for constructing lists rather than Do or For.

The simplest method of plotting a sequence of plots is as Artes said: use Table. Between the two forms he proposes, I prefer the second form as it plots the functions on the same graph, but the first form is more akin to what you were attempting. As an alternative, you could Map Plot over the values of n

Plot[Sqrt[(1/#) x], {x, 0, 1}]& /@ Range[1,5]


or,

Plot[ Evaluate[ Sqrt[(1/#) x]& /@ Range[1,5] ], {x, 0, 1}]


Additionally, you should look at Manipulate which allows you to manipulate the plot directly.

Manipulate[Plot[Sqrt[x/n], {x, 0, 1}, PlotRange -> {0, 1}], {n, 1, 5}]


gives

Note the use of PlotRange which fixes the y-axis to a specific range. If it is not present, the plot looks identical, but the y-axis changes in scale. Lastly, there's animating the whole thing, as R.M suggested.

As an aside, the above code is what one would usually use if they want to create a new List of objects. But, expressions can be constructed where both Do and For will in effect create a new List. The secret is to use Append or AppendTo on a List that already exists, or possibly using Reap and Sow.

Using your code for Do as an example,

 lst = {};
Do[AppendTo[lst, Plot[Sqrt[(1/n) x] , {x, 0, 1}]], {n, 1, 5}]


or, more verbosely

 lst = {};
Do[lst = Append[lst, Plot[Sqrt[(1/n) x], {x, 0, 1}]], {n, 1, 5}]


and, most "simply"

 Reap[Do[Sow[Plot[Sqrt[(1/n) x], {x, 0, 1}]], {n, 1, 5}]][[2]]

• Good answer explaining why OP's code doesn't work. The rest of us focused on the final objective and neglected the root of the OP's confusion :)
– rm -rf
Feb 20, 2012 at 19:31

Here is the really quick way: In your first Do loop, you can force Plot to create output by adding Print in front of it:

Do[Print@Plot[Sqrt[(1/n) x], {x, 0, 1}], {n, 1, 5}]


Now you decide you want to animate the result just to get an overview of the plots, but don't want to redo the loop. To do that, select all the output graphics by clicking the cell bracket containing them, then press Comand-y (or Control-y depending on the platform). This is also available under the menu Graphics > Rendering > Animate selected graphics.

This is what you do to just show the variations in a plot, as you asked. It's not the best choice if you want to permanently create a movie, of course.

Edit for version 10

As noted in the comment, the menu item for animating a selected cell group isn't present in version 10 anymore. Therefore, if you want a quick way to create an animation of an arbitrary selection of cells, it has to be implemented using notebook manipulation. This may still sometimes be nice to have, instead of being forced to construct the list of frames by manual copy-pasting.

So I made a button for it:

CreatePalette[{
Button["Animate Selection",
With[{l =
ToExpression@
BoxData[z_] :> z, Infinity]},
CreateDocument[ListAnimate[l], WindowSize -> All]]]}];


This palette lets you take the same output as generated in the example above, and convert it to a movie in a new window by pressing the "Animate Selection" button.

• Is this still possible in Mathematica 10.x? Sep 9, 2015 at 15:37
• I have no "Rendering" submenu in my Graphics menu, and Control-y is not doing anything (Windows 7, Mathematica v10.2). I also could not find a FrontEnd Token to trigger this. Sep 9, 2015 at 17:08
• @GustavoDelfino I've updated my answer to get a similar functionality in newer versions of Mathematica.
– Jens
Sep 9, 2015 at 22:21
• Thank you. That is a nice palette button! Sep 10, 2015 at 18:02

Try one of the following:

Table[ Plot[ Sqrt[(1/n) x], {x, 0, 1}], {n, 1, 5}]


or

 Plot[ Evaluate@Table[ Tooltip@Sqrt[(1/n) x], {n, 1, 5}], {x, 0, 1}]


The first method yields five different plots (as you seemed to expect; you don't need to sety = Sqrt[(1/n) x] in Plot since you don't make any use of it), while the latter visalizes differences between functions in one plot, where Tooltip makes show formulas of given functions.

Edit

One could follow this way further to make a visualization a bit more friendly like in R.M.'s answer :

Animate[
Show[{Plot[Sqrt[x/k], {x, 0, 1}, PlotStyle -> Thick],
Plot[Evaluate@Table[Sqrt[x/n], {n, 1, 5}],
{x, 0, 1}, PlotStyle -> Thick]},
PlotRange -> {0, 1}], {k, 1, 5}]