4
$\begingroup$

ListPlot needs a list (duh). Assume that you have a problem where time is not the issue, but space, e.g. compute and plot the first billion values of Fibonacci[n]/GoldenRatio^n. Iteratively you can do it in O(1) time for each step (if you cheat with GoldenRatio - it's just an example). Storing all these values in a list to plot it...just no.

Thus, can you do something like ListPlot with the list being generated on the fly, the plot function just looking at the element currently generated? (Related note, wrong SE: can Python's matlibplot do it? You could put that into comments if you happen to know.)

$\endgroup$
0

2 Answers 2

5
$\begingroup$

This solution uses the "RingBuffer" data structure.

Dynamic[ds];
npts = 16;
xticks = Transpose[{Range[1, npts], ToString /@ Range[-npts, -1]}];

Initialize:

ds = CreateDataStructure["RingBuffer", npts];
Do[ds["PushBack", i], {i, npts}];
ds["Elements"]

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}

ListPlot updates with a Refresh interval of 0.5s:

Dynamic[
 Refresh[
  ListLinePlot[First@Normal[ds]
   , PlotStyle -> {Thick, Red}
   , Ticks -> {xticks, Automatic}
   ], UpdateInterval -> 0.5]
 ]

Test: (add 10 values)

Do[
 ds["PushBack", RandomInteger[{1, 7}]];
 Pause[0.5],
 {i, 1, 10}
 ]

Result: (starts with the values from 1-16 initially and then the recently entered points are kept only.

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ You beat me to it. +1 $\endgroup$
    – Edmund
    Feb 27, 2022 at 14:43
  • $\begingroup$ I upped this one as it is immediately clear to me what a ring buffer does. Since both offers use "Dynamic", I give that function a closer look :-) $\endgroup$ Feb 28, 2022 at 9:20
3
$\begingroup$

Here is an example using "Dynamic". We simulate the long calculation by "Pause":

x = 1;
tot = Graphics[Point[{x, x^2}]];
next[t_] := (tot = 
     Show[tot, Graphics[Point[{++x, x^2}]], Axes -> True, 
      AspectRatio -> 1];);
Dynamic[tot]
Do[next[tot]; Pause[0.5];, {20}]

enter image description here

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.