17

I am wondering if you have good ways to show plots inside a loop in mma. Usually, the output of Plot function is not shown, for example in the following code:

For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}]]

Thanks for your help.

Edit

In connection to my previous question, I already have the For loop, for example, like this For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}]]. Given this fact, I want to have something like "press any key to continue..." inside the For loop, then refresh the plot every time I press any random key. Could anybody give a complete working code?

2
  • 2
    Does it have to be a keypress, and only moving forward? There's always Manipulate. Commented Apr 6, 2011 at 22:16
  • @Jefromi: Yes, ideally I want to have key press. Commented Apr 6, 2011 at 22:21

3 Answers 3

21

Just use Print:

For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}] // Print]

or Monitor:

Monitor[For[i = 1, i <= 10, i++, p = Plot[Sin[i*x], {x, -Pi, Pi}]; 
  Pause[0.5]], p]

(Pause is used here to give some time to view the plot; the loop is pretty fast here. Remove if necessary)

EDIT
On request a version that is controlled by mouse clicks on the graph (key presses need the graph to have focus so you need to click anyway)

Monitor[For[i = 1, i <= 10, , p = Plot[Sin[i*x], {x, -Pi, Pi}]], 
EventHandler[p, {"MouseDown" :> i++}]]

This is a pretty stupid way to do this. The loop redraws the plot continuously. So, a slightly (but still ugly) version might be:

s = True;
Monitor[
 For[i = 1, i <= 10, ,
  If[s,
   (* Put main loop body here*) 
   p = Plot[Sin[i*x], {x, -Pi, Pi}] 
   (* end of main body *) ;
   s = False (* prevents continuous re-evaluating main body *)
   ]
  ]
 , EventHandler[p, {"MouseDown" :> (i++; s = True)}]
 ]
Sign up to request clarification or add additional context in comments.

10 Comments

@Sjoerd C. de Vries: thank you. This does plot, but it generates a lot of plots. Ideally I want the plot to refresh after I press any key. Do you know how to achieve this effect?
@Qiang You changed your question after I gave this answer. Do you still want it inside a For loop? In the meantime Michael already provide an answer without it.
@Sjoerd C. de Vries: Yes, I still want it inside a For loop.
@Sjoerd: It's tricky to get this working with a keyboard event, because the notebook focus moves off the plot. Trying to combine it with a NotebookMove[] runs into problems with not actually displaying the Plot. The only way I could work around it was with explicit (cell) print and cell delete commands...
@Simon you can also use PrintTemporary which acts like Print but returns a NotebookInterfaceObject you can pass to NotebookDelete in the same evaluation, removing the cell. Avoids the need to use SelectionMove.
|
15

Just return a list of the plots, instead of using a For loop:

Table[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10}]

enter image description here

If you want them all concatenated as one plot, Show[listOfPlots] is one way to do that:

Show[Table[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10}]]

enter image description here

UPDATE

Here's one simple way using Dynamic and EventHandler:

DynamicModule[{i = 1},
 EventHandler[Dynamic[Plot[Sin[i*x], {x, -Pi, Pi}]],
  {"KeyDown" :> i++}
  ]

And here's a slightly more fancy interface made with Animate:

Animate[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10, 1}, AnimationRunning -> False]

2 Comments

thanks for your answer. I have editted my post, hopefully this time it is more clear.
I added an example in my edit. Don't be afraid to use the built-in tools for this sort of thing either, like Animate and Manipulate.
6

If you really want to have the user press a key between Plots, the simplest way might be

For[i = 1, i <= 10, i++, 
    If[!ChoiceDialog[Plot[Sin[i*x], {x, -Pi, Pi}], 
         WindowTitle -> "Plot #" <> ToString[i] 
                                 <> ":  Press OK or Enter to continue"],
    Abort[]]]

enter image description here

4 Comments

I keep pressing the OK button and nothing happens. BTW the cancel button doesn't work either. Is it a bug on your program, or just a Mozilla compatibility issue?
@belisarius: You had me going for a second... :P
Simon, congratulations on breaking 3K rep.
@Mr.Wizard: Thanks! You're catching up fast though... you're in the top 2% this month.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.