# Plot inside a loop again\[, but now in Pluto\]

**URL:** <https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295>\
**Category:** New to Julia\
**Tags:** plots, pluto\
**Created:** [October 30, 2020, 11:03am UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295 "2020-10-30T11:03:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dsantra92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dsantra92/32/21432_2.png) [@Dsantra92](https://discourse.julialang.org/u/Dsantra92)\
**Post date:** [October 30, 2020, 11:03am UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295/1 "2020-10-30T11:03:13Z")

</div>

```julia
using Plots;plotly()

begin
	plot()
	for i = 0:0.1:1
		vline!([i], leg = :none)
		sleep(0.01)
	end
	#plot!()
end

```

I want to the plot for for each iteration of the loop.How can I do it?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [October 30, 2020, 12:46pm UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295/2 "2020-10-30T12:46:13Z")

</div>

This works fine for me:

```julia
begin
	p = plot()
	for i = 0:0.1:1
		vline!(p, [i], leg = :none)
		sleep(0.01)
	end
	p
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:** [October 30, 2020, 3:13pm UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295/3 "2020-10-30T15:13:29Z")

</div>

If I understand OP correctly, he wants to show the plot in each iteration? I.e. this:

```julia
p = plot()
for i = 0:0.1:1
	vline!(p, [i], leg = :none)
	sleep(0.1)
	display(p)
end
p

```

(note the `display` call), which works fine for me in Juno, but not in Pluto for some reason.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [October 30, 2020, 6:58pm UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295/4 "2020-10-30T18:58:57Z")

</div>

In that case I would probably take advantage of Pluto’s reactive nature and create a `Slider(0:0.1:1)` via PlutoUI and then view the plot that way…

---

<div class="post-metadata">

**Author:** ![fonsp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fonsp/32/222349_2.png) [@fonsp](https://discourse.julialang.org/u/fonsp)\
**Post date:** [October 31, 2020, 1:37pm UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295/5 "2020-10-31T13:37:56Z")

</div>

```julia
T = 0:0.1:1

```

```julia
@bind N Slider(eachindex(T))

```

```julia
let
p = plot()
for i = T[1:N]
	vline!(p, [i], leg = :none)
end
p
end

```

---

<div class="post-metadata">

**Author:** ![Dsantra92](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dsantra92/32/21432_2.png) [@Dsantra92](https://discourse.julialang.org/u/Dsantra92)\
**Post date:** [December 2, 2020, 8:05am UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295/6 "2020-12-02T08:05:51Z")

</div>

That is a great of doing the plots, but I want to plot live data. The solution here does my job what if I want to display the plots before the all the loops are computed ?

A simple use case is gradient descent. We want to plot live data of loss v/s epoch of the model being trained. Based on the live graph we can decide whether to continue or stop the rest of the computation.

Is there any way I can do it ?

---

<div class="post-metadata">

**Author:** ![fonsp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fonsp/32/222349_2.png) [@fonsp](https://discourse.julialang.org/u/fonsp)\
**Post date:** [December 16, 2020, 8:04pm UTC](https://discourse.julialang.org/t/plot-inside-a-loop-again-but-now-in-pluto/49295/7 "2020-12-16T20:04:00Z")

</div>

I wrote a minimal demo about this once:  
[https://github.com/fonsp/disorganised-mess/blob/master/epoch.jl](https://github.com/fonsp/disorganised-mess/blob/master/epoch.jl)
