How to generate gif from a vector of pre-made plots and save to file?

Hi, I’m trying to generate a gif animation from a sequence of plots. Instead of using the @animate or @gif macro while plotting in a loop, all plots are produced ahead of time and saved to a vector of type Plots.Plot. I would like to turn this vector of plots into a gif file. I was able to do as follows:

anim=@animate for p in plot_vector
       plot(p)
       end
gif(anim, "my_anim.gif", fps=10)

but I feel like this should be done in one line. I searched around but did not find a better solution.
Does anyone know how to do this? Thanks a lot

To create a gif file from pngs generated by other plotting library than Plots.jl, proceed as follows:

  • save the pngs with the name in the form: “000001.png”, “000002.png”, … “000030.png”, i.e. with the format “%06d.png”.
    If the pngs are already created and saved in a folder, images, you have to rename them, because otherwise their order cannot be deduced.
  • create a vector of strings, fnames, containing the png filenames:
import Plots: Animation, buildanimation
fnames = String[]
for k in 1:numberofframes
    filename=lpad(k, 6, "0")*".png"
    push!(fnames, filename)
end
anim = Animation("images", fnames)
buildanimation(anim, "images/mygif.gif", fps = 12, show_msg=false)
2 Likes

I figured out a way as follows:

anim = animation(plot_vector, "mygif.gif", fps=12)

Here plot_vector::Vector{Plots.Plot} is a vector of pre-made plots.