Plotting from within a loop using GR?

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

histogram(randn(10))

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

why is that?

2 Likes

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

p = histogram(randn(10))

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

p

yields

4 Likes

GR:

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

Plots + gr():

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

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.

1 Like

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:

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

maybe this discussion helps:

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

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!

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

3 Likes

Thank you! I was searching for this.