# Plotting distributions with StatsPlots in a for loop

**URL:** https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748
**Category:** Visualization
**Tags:** distributions
**Created:** [February 21, 2021, 11:11pm UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748 "2021-02-21T23:11:30Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [February 21, 2021, 11:11pm UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/1 "2021-02-21T23:11:31Z")

</div>

Hey guys! I need to understand why this does not plot inside a `for` loop but does plot ok outside:

```julia
using Distributions, StatsPlots

dists = Iterators.product(0:1:2, 0:0.2:1) |> collect

for i in dists
    plot!(Normal(i[1], i[2])) # also tried with plot() and also tried instantiating a plot() before the loop
end

```

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 22, 2021, 12:19am UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/2 "2021-02-22T00:19:53Z")

</div>

One way is to add a `plot!()` at the end:

```julia
julia> x = rand(10);

julia> plot()

julia> for i in 1:3
         plot!(x)
       end

julia> plot!()

```

Maybe this is more elegant and useful in some situations:

```julia
julia> p = plot();

julia> for i in 1:3
         plot!(p,rand(10))
       end

julia> plot!(p)

```

---

<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: [February 22, 2021, 12:23am UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/3 "2021-02-22T00:23:09Z")

</div>

Try inserting the `plot!` command inside a `display()` command.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [February 22, 2021, 12:24am UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/4 "2021-02-22T00:24:13Z")

</div>

Yes, it works 🙂

```julia
julia> plot()

julia> for i in 1:3
          display(plot!(rand(10)))
       end

```

---

<div class="post-metadata">

### Author: ![Storopoli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/storopoli/32/209278_2.png) [@Storopoli](https://discourse.julialang.org/u/Storopoli)
#### Post date: [February 22, 2021, 7:59am UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/5 "2021-02-22T07:59:00Z")

</div>

thank you all!

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 22, 2021, 8:07am UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/6 "2021-02-22T08:07:00Z")

</div>

I think the “official” way of doing this is `current()`

---

<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: [February 22, 2021, 10:10am UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/7 "2021-02-22T10:10:26Z")

</div>

Testing `current()` it seems to display only the final result after the loop, but not the intermediate plot updates, which can be of interest to visualize animations with some delay between plots:

```julia
plot()
for i in 1:5
    sleep(0.5)
    display(plot!(rand(10)))
end

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [February 22, 2021, 10:25am UTC](https://discourse.julialang.org/t/plotting-distributions-with-statsplots-in-a-for-loop/55748/8 "2021-02-22T10:25:36Z")

</div>

Yes sorry I read this as the classic “I’m plotting in a loop and can’t see the output” issue, for intermediate plots indeed display is the way to go
