# Plotting from within a loop using GR?

**URL:** <https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435>\
**Category:** Visualization\
**Tags:** question, plotting\
**Created:** [June 24, 2017, 12:39am UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435 "2017-06-24T00:39:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zorp](https://avatars.discourse-cdn.com/v4/letter/z/c2a13f/32.png) [@zorp](https://discourse.julialang.org/u/zorp)\
**Post date:** [June 24, 2017, 12:39am UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/1 "2017-06-24T00:39:48Z")

</div>

The following doesn’t produce any output using Juno and v0.6 of Julia:

```julia
histogram(randn(10))

for i = 1:10
	histogram!(randn(10))
end

```

why is that?

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 24, 2017, 12:51am UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/2 "2017-06-24T00:51:54Z")

</div>

Don’t you need to add TO an existing plot?

```julia
p = histogram(randn(10))

for i in 1:10 
  histogram!(p, randn(10))
end

p

```

yields

![](https://global.discourse-cdn.com/julialang/original/3X/1/b/1be606b00857f949476002a5cf4f369d1427ee33.png)

![](https://global.discourse-cdn.com/julialang/original/3X/2/f/2f6206f308396003b33f64411ed437332de5ff2f.png)

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [June 24, 2017, 1:12am UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/3 "2017-06-24T01:12:17Z")

</div>

GR:

```julia
using GR
inline("atom")
for i in 1:100
  histogram(randn(10000))
end

```

Plots + gr():

```julia
using Plots
gr()
for i in 1:100
  display(histogram(randn(10000)))
end

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [June 24, 2017, 8:02am UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/5 "2017-06-24T08:02:41Z")

</div>

If you plot from within a loop using Plots, I believe you have to call the function `gui() ` in the end of the loop for the results to be displayed.

---

<div class="post-metadata">

**Author:** ![daschw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daschw/32/2926_2.png) [@daschw](https://discourse.julialang.org/u/daschw)\
**Post date:** [June 26, 2017, 3:29pm UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/6 "2017-06-26T15:29:49Z")

</div>

If you add to a plot within a loop, no plot object is returned and thus nothing is shown. Another alternative to the above advices is returning the current plot object with `current()` after the loop:

```julia
histogram(randn(10))
for i in 1:10
    histogram!(randn(10))
end
current()

```

---

<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:** [June 27, 2017, 2:06pm UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/7 "2017-06-27T14:06:57Z")

</div>

maybe this discussion helps:

> [@On-the-fly Animation in IJulia Notebooks](https://discourse.julialang.org/t/on-the-fly-animation-in-ijulia-notebooks/524):
>
> I recently revisited how to do on-the-fly visualisation in an IJulia notebook, i.e., plotting results while the computation is still running. This was discussed a while back in [Google groups link](https://groups.google.com/forum/#!searchin/julia-users/Interactive%2420Animated%2420PyPlot%2420in%2420IJulia%7Csort:relevance/julia-users/Wb5hgyj2PMY/jtFVMvR01x0J) I’ve tried the same scheme with Plots.jl with Plotly backend, and I am thrilled that this still works. (Julia 0.5.1pre). Below is the code for anybody interested. But my key issue back then and still now is: why does it need to be so complicated? I.e. why can I not simply write x = linspace(0, 2\*pi, …

---

<div class="post-metadata">

**Author:** ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)\
**Post date:** [January 3, 2020, 3:48pm UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/8 "2020-01-03T15:48:25Z")

</div>

This do help! I applied it to plot from within two nested for loop in a function.

```julia
function bala()
 for i
    for j
        if
          plot ()
        else
          plot!()
       end
     end
   end
 return current() #return the plot!!!!
 end

```

so I can do

> p1=func() #this allow it to be used as subplot

Thank you!

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [February 28, 2020, 7:34pm UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/9 "2020-02-28T19:34:35Z")

</div>

You can also try calling `display()` on the output of the plot within the loop. That’s what I’ve been doing.

---

<div class="post-metadata">

**Author:** ![Slobodanip](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/slobodanip/32/32691_2.png) [@Slobodanip](https://discourse.julialang.org/u/Slobodanip)\
**Post date:** [January 17, 2022, 8:23pm UTC](https://discourse.julialang.org/t/plotting-from-within-a-loop-using-gr/4435/10 "2022-01-17T20:23:25Z")

</div>

Thank you! I was searching for this.
