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