Plotting "on the fly" possible?

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.)

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]
]


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.

• You beat me to it. +1 Commented Feb 27, 2022 at 14:43
• 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 :-) Commented Feb 28, 2022 at 9:20

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}]