2
$\begingroup$

I am using ListPlot to plot some data, the corresponding function is named showListPlot. Now I would like to show a series of such plots in the same window. To do that I am using the following:

Monitor[For[index = 1, index < number + 1, index++, 
  s = showListPlot[index]] , s]

I would like that monitor stops at the last plot (to see the end state) and does not vanish. How is that possible?

$\endgroup$
  • 1
    $\begingroup$ You might consider whether Animate and ListAnimate are possible alternative approaches. For loops are unnecessary and are included in Mathematica, IMO, to make it easy to translate C/Fortran code. If such a loop is needed in your actual application, consider Do. $\endgroup$ – Michael E2 Mar 3 '15 at 14:09
  • $\begingroup$ I combined you hint with kgulers code, which is also nice: lp = ListPlot[#, PlotRange -> {{0, 50}, {0, 10}}] & /@ RandomReal[10, {10, 50}]; showlp = Show[lp[[#]], PlotLabel -> Style["index = " <> ToString@#, 16, Bold]] &; number = Length[lp]; ListAnimate[Table[showlp[index], {index, 1, number}], AnimationRepetitions -> 1] $\endgroup$ – mrz Mar 3 '15 at 14:30
4
$\begingroup$
lp = ListPlot[#, PlotRange -> {{0, 50}, {0, 10}}] & /@ RandomReal[10, {10, 50}];
showlp = Show[lp[[#]], PlotLabel -> Style["index = " <> ToString@#, 16, Bold]] &;
number = Length[lp];

Update: If you don't have to use Monitor, there are a number of alternatives including Dynamic and Clock combination:

Dynamic@With[{c = Clock[{1, number, 1}, 5, 1]}, showlp[c]]

enter image description here

Original post:

Monitor[For[index = 1, index < number, Pause[.3]; index++, s = showlp[index]], s]; showlp[number]

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you kguler. I have the impression that this routine is showing the last plot twice? $\endgroup$ – mrz Mar 3 '15 at 13:57
  • 1
    $\begingroup$ @MilenkoRubin-Zuzic, you are right. If that is an issue, you can change number+1 to number and s at the end to showlp[number]. $\endgroup$ – kglr Mar 3 '15 at 14:01
  • $\begingroup$ That's it. Why so easy ... can you change the upper code , so that I can vote for the answer? $\endgroup$ – mrz Mar 3 '15 at 14:14
  • $\begingroup$ @Milenko, just updated the answer. $\endgroup$ – kglr Mar 3 '15 at 14:19
  • $\begingroup$ Michael E2 had the idea tu use ListAnimate. So we could also could replace the line with Monitor[] ... to: ListAnimate[Table[showlp[index], {index, 1, number}], AnimationRepetitions -> 1]. Also nice ... $\endgroup$ – mrz Mar 3 '15 at 14:32
1
$\begingroup$
showListPlot[index_] := {Plot[x, {x, 0, 3}], Plot[x^2, {x, 0, 3}], Plot[x^3, {x, 0, 3}]}[[index]]

number = 3;

Block[{printFunction = PrintTemporary}, 
    Do[If[index == number, printFunction = Print]; 
        printFunction[showListPlot[index]]; Pause[1]
    , {index, number}]
]

Monitor is still better I guess, but here it goes the edit:

showListPlot[index_] := {Plot[x, {x, 0, 3}], Plot[x^2, {x, 0, 3}], Plot[x^3, {x, 0, 3}]}[[index]]

number = 3;

PrintAndDelete[expression_]:=Module[{cell},cell=PrintTemporary[expression];Pause[1];NotebookDelete[cell]]

Module[{printFunction=PrintAndDelete},
    Do[If[index==number,printFunction=Print];printFunction[showListPlot[index]],{index, number}]
]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Dear Giovanni, I would like to see the curves in the "same window cell" and that the last plot is not cleared. $\endgroup$ – mrz Mar 3 '15 at 14:10
  • $\begingroup$ Dear Giovanni, interesting ... I did not know this ... thanks $\endgroup$ – mrz Mar 3 '15 at 14:44

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.