# Plotting in a loop

**URL:** <https://discourse.julialang.org/t/plotting-in-a-loop/10074>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [March 30, 2018, 1:54pm UTC](https://discourse.julialang.org/t/plotting-in-a-loop/10074 "2018-03-30T13:54:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leoll2](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@leoll2](https://discourse.julialang.org/u/leoll2)\
**Post date:** [March 30, 2018, 1:54pm UTC](https://discourse.julialang.org/t/plotting-in-a-loop/10074/1 "2018-03-30T13:54:11Z")

</div>

I have some data that are updated in a for-loop. I need to plot them for each iteration of the cycle, in the same graph (that is, clear the old data and plot the new).  
For sure this problem is very common and easy to solve, however I can’t find any straightforward example online.  
I’m using Jupyter as environment and PyPlot for “static” graphs (I mean, diagrams that don’t change over time).

Here’s a pseudo-code to understand what I need:

```julia
for i in 1:100
  clear_plot()
  plot(sin(i*x))
  sleep(1)
end

```

The above code should plot a sine wave, wait 1 second, replace it with a different one, wait another second, and so on.  
Can you help me? Thanks!

---

<div class="post-metadata">

**Author:** ![cortner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cortner/32/204_2.png) [@cortner](https://discourse.julialang.org/u/cortner)\
**Post date:** [March 30, 2018, 2:24pm UTC](https://discourse.julialang.org/t/plotting-in-a-loop/10074/2 "2018-03-30T14:24:05Z")

</div>

You want `IJulia.clear_output(true)`

See [here](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524/43) for a fairly extensive discussion of your question.

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [March 30, 2018, 8:14pm UTC](https://discourse.julialang.org/t/plotting-in-a-loop/10074/3 "2018-03-30T20:14:39Z")

</div>

Aren’t you refering to `Plots.display(plot(sin(i*x)))` to update the plot within the loop?

---

<div class="post-metadata">

**Author:** ![leoll2](https://avatars.discourse-cdn.com/v4/letter/l/22d042/32.png) [@leoll2](https://discourse.julialang.org/u/leoll2)\
**Post date:** [March 31, 2018, 10:24am UTC](https://discourse.julialang.org/t/plotting-in-a-loop/10074/4 "2018-03-31T10:24:31Z")

</div>

In my notebook it doesn’t update the plot, instead it creates a new plot under the previous one.  
A working solution, as pointed out by @cortner, is `IJulia.clear_output(true)`

Here’s a complete code that works perfectly in Jupyter, I hope it helps other newbies like me:

```julia
using Plots
for i in 1:20
    IJulia.clear_output(true)
    x = linspace(0,2*pi,1000); y = sin.(i*x)
    Plots.display(plot(x,y, color="red"))
    sleep(0.1)
end
println("Done!")

```
