Need help with makie animation

This is my code :

I want to create an animation or slide illustrating the movement of my points on a sphere as the variable ‘i’ increases from 1 to ‘Stop’. This will allow me to visualize the trajectory of my three points on the sphere. However, I’m not familiar with the process. Can anyone provide tips on how to achieve this?

Currently, my plot resembles this:

And this is incorrect because it depicts the movement of my three points in a single picture, whereas I intend to spread it out across 35 pictures (as my ‘Stop’ value is defined as 35).

You should take a look at Makie’s animation docs

In this case, you can update the contents of a single scatter plot directly, instead of re-plotting.

fig1, ax1, plt1 = mesh(...)
data_PSO = ...
moving_scatter_plot = scatter!(ax1, Point3f.(data_PSO[1]); markersize = 10, color = :blue)

record(fig1, "animation.gif", 1:Stop; framerate = 10) do i
    moving_scatter_plot[1][] = Point3f.(data_PSO[i])
    if i == Stop
        moving_scatter_plot.markersize = 20
        moving_scatter_plot.color = :red
    end
end

This should give you a GIF saved to animation.gif!

For more complex animations, the better way to handle this is through observables:

fig1, ax1, plt1 = mesh(...)
data_PSO = ...
# new line
data_points = Observable(Point3f.(data_PSO[1]))
moving_scatter_plot = scatter!(ax1, data_points; markersize = 10, color = :blue)

record(fig1, "animation.gif", 1:Stop; framerate = 10) do i
    data_points[] = Point3f.(data_PSO[i])
    if i == Stop
        moving_scatter_plot.markersize = 20
        moving_scatter_plot.color = :red
    end
end
1 Like