Creating a real-time film

Hi. I’m relatively new to Julia, but I’m writing a course for students that involves animating plots, so for example plotting a sequence of graphs showing a sine curve that is successively moved to the right, so that it creates an impression of a moving wave. So what I really need is a way of repainting the contents of an existing figure without completely replacing the figure. However, I just can’t seem to find what I need.

I’m afraid I don’t even know the right word for what I’m looking for. When I look up “animation”, that seems to mean creating a complete gif, but for my more complex systems, that would mean accumulating large amounts of data that I don’t need: I only want to see the current state of the system, and throw away the past.

Can anyone help?

Thanks,
Niall.

Have you seen GLMakie for example? Animations

You can record such animations or just let them play while looking at them

Thanks very much - that looks like just what I’m looking for.

I am surprised, though, that it seems such a big deal. In Matlab I was used to simply redrawing a figure, but here I need to used dedicated macros and Observables. How does that come about?

It’s not a big deal really, Observables are a way of making it easier to let information changes flow to the display.

The minimal example for the sine would be:

phase = Observable(0.0)

x = 0:0.01:10
y = lift(phase) do p
    @. sin(x - p)
end

fig, ax, lin = lines(x, y)
display(fig)

for _phase in 0:0.1:10
    phase[] = _phase
    sleep(1/60)
end

So you update phase, which updates y, which updates the plot.

1 Like

Great - thanks a lot. I’ll have to look into this. :grin:

Just checking, but should that phase inside the loop be _phase?

Ah yeah good point, didn’t run that :slight_smile:

1 Like

Hey, that’s really neat - thanks a lot. Plus the code has a bunch of features that are new to me and it’ll be fun working them out. :smiley: