# How to animate a plot with subplots

**URL:** <https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831>\
**Category:** Visualization\
**Tags:** plots, animations\
**Created:** [January 18, 2022, 8:54pm UTC](https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831 "2022-01-18T20:54:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [January 18, 2022, 8:54pm UTC](https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831/1 "2022-01-18T20:54:12Z")

</div>

I’d like to make an animation of a figure with two subplots.

```julia
using Plots

x_data = range(0.0,stop=2π, length=1000)
y_data = sin.(x_data) .+ 0.1.*randn(1000,100)

@gif for i ∈ 1:100
    fig = plot(layout = grid(2,1), legend=false)

    plot(fig[1], 
        x_data, y_data)
    vline!([x_data[i]])

    # histogram!(fig[2],y_data[i,:])
end

```

Gets this:

![download](https://global.discourse-cdn.com/julialang/original/3X/7/2/728f13b96145456be733fcc9ff974cb7ab7bbb0f.gif)

If I remove the comment from the histogram line, I get this:  
 ![download (1)](https://global.discourse-cdn.com/julialang/original/3X/4/2/4214e40ed69e8d958fb98114415c6e84a11830cc.gif)

How can I have both subplots show in the animation?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2022, 9:10pm UTC](https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831/2 "2022-01-18T21:10:05Z")

</div>

This [other thread](https://discourse.julialang.org/t/how-to-do-multiple-cameras-in-subplots-with-plot3d/65940/2) has a very nice example of animated subplots.

---

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [January 18, 2022, 9:14pm UTC](https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831/3 "2022-01-18T21:14:16Z")

</div>

I also just realized I was missing a `!` after my first plot 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [January 18, 2022, 9:17pm UTC](https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831/4 "2022-01-18T21:17:31Z")

</div>

You think I should delete this thread since it was just a simple syntax error? I did make note of that other one you linked to, and it’s helpful for making things look really nice!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [January 18, 2022, 9:20pm UTC](https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831/5 "2022-01-18T21:20:03Z")

</div>

I would leave it and post the corrected code and the final animation. It will surely be useful to others.

---

<div class="post-metadata">

**Author:** ![thompsonmj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thompsonmj/32/25172_2.png) [@thompsonmj](https://discourse.julialang.org/u/thompsonmj)\
**Post date:** [January 18, 2022, 9:27pm UTC](https://discourse.julialang.org/t/how-to-animate-a-plot-with-subplots/74831/6 "2022-01-18T21:27:44Z")

</div>

```julia
using Plots

x_data = range(0.0,stop=2π, length=1000)
y_data = sin.(x_data) .+ 0.1.*randn(1000,100)

@gif for i ∈ 1:100
    fig = plot(layout = grid(2,1), legend=false)

    plot!(fig[1], # ADD !
        x_data, y_data)
    vline!([x_data[i]])

    histogram!(fig[2],y_data[i,:],
    xlim=(-1.2,1.2), ylim=(0,50)) # Also added to make it less jumpy.
end

```

![download (1)](https://global.discourse-cdn.com/julialang/original/3X/8/4/84fd32eb7f65d7d6ad9df1b1dacd31f385defaa8.gif)
