# Need help animating a plot

**URL:** <https://discourse.julialang.org/t/need-help-animating-a-plot/47956>\
**Category:** New to Julia\
**Tags:** question, plotting\
**Created:** [October 7, 2020, 9:56pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956 "2020-10-07T21:56:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [October 7, 2020, 9:56pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/1 "2020-10-07T21:56:32Z")

</div>

I have a simple line plot that I would like to animate.  
An array with the number of iterations and the associated array with a value associated with the current iteration.

```nohighlight
iters = [x for x in 1:100];
vals = rand(100);

```

I simply want to animate the development of vals output at each iteration - tiers.  
How can I do this in Julia?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 7, 2020, 10:12pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/2 "2020-10-07T22:12:12Z")

</div>

using Plots.jl? [Animations · Plots](https://docs.juliaplots.org/latest/animations/)

---

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [October 7, 2020, 10:14pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/3 "2020-10-07T22:14:10Z")

</div>

I don’t mind whichever plotting library. I looked at that page but I couldn’t really connect much with the example given hence why I am here.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 7, 2020, 10:21pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/4 "2020-10-07T22:21:17Z")

</div>

if you want to plot for each `val` and animate, just

```julia
@gif for i in vals
    plot(...use your i here like you normally would...)
end

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 7, 2020, 11:32pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/5 "2020-10-07T23:32:08Z")

</div>

You can use a slider with Interact.jl, or in the pluto notebook.

---

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [October 8, 2020, 12:39pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/6 "2020-10-08T12:39:44Z")

</div>

> [@jling](#):
>
> ```julia
> @gif for i in vals
> plot(...use your i here like you normally would...)
> end
> 
> ```

Going by this template, I did this which fails miserably:

```julia
@gif for i in vals
    plot(i)
end

```

Any pointers?

---

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [October 8, 2020, 12:46pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/7 "2020-10-08T12:46:57Z")

</div>

> [@dpsanders](#):
>
> You can use a slider with Interact.jl, or in the pluto notebook.

That’s also a very interesting approach. How would that work with this basic example?

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [October 8, 2020, 12:48pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/8 "2020-10-08T12:48:50Z")

</div>

Is this what you’re after?

```julia
using Plots
vals = rand(100)
@gif for i in eachindex(vals)
    plot(vals[1:i])
end

```

---

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [October 8, 2020, 12:53pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/9 "2020-10-08T12:53:08Z")

</div>

> [@kdyrhage](#):
>
> ```julia
> using Plots
> vals = rand(100)
> @gif for i in eachindex(vals)
> plot(vals(1:i))
> end
> 
> ```

Yes! Small typo `plot(vals(1:i))` should be `plot(vals[1:i])`, right?

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [October 8, 2020, 12:53pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/10 "2020-10-08T12:53:45Z")

</div>

> [@PyDataBlog](#):
>
> Small typo `plot(vals(1:i))` should be `plot(vals[1:i])` , right?

Correct, fixed it.

---

<div class="post-metadata">

**Author:** ![PyDataBlog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pydatablog/32/12327_2.png) [@PyDataBlog](https://discourse.julialang.org/u/PyDataBlog)\
**Post date:** [October 8, 2020, 12:55pm UTC](https://discourse.julialang.org/t/need-help-animating-a-plot/47956/11 "2020-10-08T12:55:25Z")

</div>

Thanks for your time and tips 🙂
