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

**URL:** https://discourse.julialang.org/t/how-to-generate-gif-from-a-vector-of-pre-made-plots-and-save-to-file/102183
**Category:** Visualization
**Created:** [July 28, 2023, 6:06am UTC](https://discourse.julialang.org/t/how-to-generate-gif-from-a-vector-of-pre-made-plots-and-save-to-file/102183 "2023-07-28T06:06:21Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jinfreedom](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jinfreedom](https://discourse.julialang.org/u/jinfreedom)
#### Post date: [July 28, 2023, 6:06am UTC](https://discourse.julialang.org/t/how-to-generate-gif-from-a-vector-of-pre-made-plots-and-save-to-file/102183/1 "2023-07-28T06:06:21Z")

</div>

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:

```julia
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

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [July 28, 2023, 5:44pm UTC](https://discourse.julialang.org/t/how-to-generate-gif-from-a-vector-of-pre-made-plots-and-save-to-file/102183/2 "2023-07-28T17:44:29Z")

</div>

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:

```julia
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)

```

---

<div class="post-metadata">

### Author: ![jinfreedom](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jinfreedom](https://discourse.julialang.org/u/jinfreedom)
#### Post date: [July 28, 2023, 10:00pm UTC](https://discourse.julialang.org/t/how-to-generate-gif-from-a-vector-of-pre-made-plots-and-save-to-file/102183/3 "2023-07-28T22:00:27Z")

</div>

I figured out a way as follows:

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

```

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