2
$\begingroup$

I have some heavy calculations with generates huge list of {x, y} points. I want to dynamically update plot during calculations. Or update it after calculations complete. I can't use simple Dynamic @ Plot, because it makes calculation time 5-10x times longer. So I made some example, how I tried to solve this problem. But it didn't work.

Manipulate[None, 
  Grid[
    {{Dynamic[
        ListPlot[
          Refresh[pts, UpdateInterval -> 5], 
          PlotRange -> All], 
        UpdateInterval -> 2], 
      SpanFromLeft, 
      SpanFromLeft}, 
     {Slider[Dynamic[a], {10, 20, 1}], 
      Dynamic@ProgressIndicator[i, {1, a}], 
      Button["Run", func[], Method -> "Queued"]}}], 
  TrackedSymbols :> {}, 
  ContinuousAction -> False, 
  SynchronousUpdating -> False, LocalizeVariables -> True, 
  Initialization :> (
    pts = {{0, 0}}; a = 10; i = 1; 
    func[] := 
     Module[{}, 
       For[i = 1;, i < a, i++, Pause[.5]; AppendTo[pts, {i, i}]]])]

So how can I refresh plot only every n seconds? Or refresh after all calculations complete?

$\endgroup$
  • 1
    $\begingroup$ Can't you just run the plot at the end of the notebook? $\endgroup$ – Feyre Jun 26 '16 at 18:13
  • 2
    $\begingroup$ ListPlot[] has to do a lot of fancy stuff. If you just need a graphical monitor, go raw: Graphics[Point[pts], Axes -> True]. $\endgroup$ – J. M. will be back soon Jun 26 '16 at 21:03
5
$\begingroup$

Your Manipulate expression works for me when I change your button specification to

Button["Run", pts = {{0, 0}}; func[], Method -> "Queued"]

However, I think it would better to replace your unnecessarily complicated Manipulate expression with a simple DynamicModule expression.

DynamicModule[{pts = {{0, 0}}, a = 10, i = 1, func},
  func[] := Do[Pause[.5]; AppendTo[pts, {i, i}], {i, a}];
  Dynamic @ Grid[
    {{ListPlot[pts, PlotRange -> All], SpanFromLeft, SpanFromLeft},
     {Slider[Dynamic[a], {10, 20, 1}], 
      ProgressIndicator[i, {1, a}],
      Button["Run", pts = {{0, 0}}; func[], Method -> "Queued"]}}]]

demo

$\endgroup$
  • $\begingroup$ U don't explain how can i fix my problem. U didn't use update interval in your example. Finger Down. $\endgroup$ – Vasily Jun 29 '16 at 6:50
4
$\begingroup$

Option 1: Use the UpdateInterval option to Dynamics, i.e.,

Dynamic[Plot[...],UpdateInterval->n,TrackedSymbols->{}]

where n is measure in seconds. Here, UpdateInterval is how often Dynamic "manually" updates the expression. By default it will also update it any time one of the symbols is changed, so you also need to set TrackedSymbols->{}.

Option 2: Use a dummy dataset in the Dynamics function, and only update that variable at the desired pace. For example,

Print[Dynamic[Plot[dummyData,...]]]; ...calculation code...; dummyData=newData;
$\endgroup$
  • $\begingroup$ I updated this answer to reflect the issue Wjx pointed out. $\endgroup$ – Jess Riedel Jun 27 '16 at 5:49
  • $\begingroup$ When i used Option 1: Dynamic[ListPlot[pts, PlotRange -> All], UpdateInterval -> 2, TrackedSymbols -> {}] Plot is always empty. It doen't refresh. Option 2 can work, but not good because of huge variables. Memory is limited. But thx. $\endgroup$ – Vasily Jun 29 '16 at 6:57
  • $\begingroup$ I just doubled checked with the code from your comment, and it works for me. Did you try it without Dynamic? Did you try it with both UpdateInterval and TrackedSymbols removed. Are you sure pts is good data, so that ListPlot[pts] works? $\endgroup$ – Jess Riedel Jun 29 '16 at 21:34
2
$\begingroup$

Though similar to @Jess's answer, I think this will work as you want.

Dynamic[PLOT,UpdateInterval->10,TrackedSymbols->{}]

Because UpdateInterval only set the Max Update interval!! So as your data is updating at a almost crazy speed, the plot will sense the change of your data and update crazily as well. You have to manually tell Dynamic: don't track data, simply follow the UpdateInterval!!!

Hope this can help u.

$\endgroup$
  • $\begingroup$ Yea, this is tweak is necessary. Hope you don't mind I update my answer. $\endgroup$ – Jess Riedel Jun 27 '16 at 5:46
  • $\begingroup$ @JessRiedel Don't mind at all~ $\endgroup$ – Wjx Jun 27 '16 at 7:06

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.