Simple animation with Makie

I would like to generate a gif (or mkv) from a loop within which a figure is created at each iteration. Most Makie example I saw first create a figure, then modify it in an iteration. Could someone help me getting this Plots.jl MWE into a Makie one?

using Plots
let
    anim = @animate for it=1:10
        p = plot(title="Animation")
        p = plot(1:10, (1:10).^2,  label=:none)
        p = scatter!([it], [it^2], label=:none)
    end
    gif(anim, "anim_MWE.gif", fps = 5)
end

You’ll get the closest with the new SpecApi:

using GLMakie
import Makie.SpecApi as S

let
    fig, _, spec = plot(S.GridLayout()) # create empty spec plot 
    record(fig, "anim_MWE.gif", 1:10) do it
        # update the spec
        spec[1] = S.GridLayout(S.Axis(title="Animation", plots=[
            S.Lines(1:10, (1:10).^2),
            S.Scatter([it], [it^2], )
        ]))
    end
end

It’s still a bit cumbersome in comparison, but could be made nicer for such animations.
Otherwise, for the old fashioned approach, I’d have a look at the docs:
https://docs.makie.org/stable/explanations/animation/

1 Like

Thanks, very nice new feature!
How easy would it be to incorporate subplots like in the example below?

using Plots
let
    anim = @animate for it=1:10
        p1 = plot(title="Animation 1")
        p1 = plot(1:10, (1:10).^2,  label=:none)
        p1 = scatter!([it], [it^2], label=:none)
        p2 = plot(title="Animation 2")
        p2 = plot(1:10, (1:10).^-2,  label=:none)
        p2 = scatter!([it], [it^-2], label=:none)
        plot(p1, p2)
    end
    gif(anim, "anim_MWE.gif", fps = 5)
end

I’ve tried adding fig[1, 1] in the Axis call, without success.

Like this:

let
import Makie.SpecApi as S
let
    fig, _, spec = plot(S.GridLayout()) # create empty spec plot 
    record(fig, "anim_MWE.gif", 1:10) do it
        # update the spec
        ax1 = S.Axis(title="Animation", plots=[
            S.Lines(1:10, (1:10) .^ 2),
            S.Scatter([it], [it^2],)
        ])
        ax2 = S.Axis(title="Animation 2", plots=[
            S.Lines(1:10, (1:10) .^ 2),
            S.Scatter([it], [it^2],)
        ])
        spec[1] = S.GridLayout(ax1, ax2)
    end
end


end

There are a few examples in the linked docs about how to create layouts

1 Like

I would like to ask if this is still the preferred way for doing this, or did things change over time?
I have given a try recently by I did not mange to get things to work. Thanks !

You should make a new post explaining what you want to do and what you have tried so far and what the problem right now is.

Oh sorry, I didn’t want to make a new post to ask for the same thing again. I was just curious to know if the API (SpecApi) changed in the meanwhile. I know that things are moving fast on your side and I thought that the way to create gifs using a loop may have evolved, please forget about my question.

You definitely can still do it that way, and if its the right way depends heavily on what youre doing.
You can take a look at the documentation for more infos:

1 Like

A simpler way is just

fig,ax,plt = lines(1:10, (1:10).^2)
it = Observable(1)
scatter!((@lift ($it, $it^2)))
Record(fig, 1:10) do _it
	it[] = _it
end