BBloggsbott
BBloggsbott

Reputation: 388

Store plots in an array

I am trying to plot histograms of different columns of a dataframe in subplots.

plt_count = 1
for i = names(abalone)[2:end]
    p[plt_count]=histogram(abalone[:,i])
    plt_count += 1
end
plot(p, layout=(3,3), legend=false)

This is what I tried. But I can't come up with the right definition for the array p. How do I define p?

Improvements to the code will also be helpful.

Upvotes: 2

Views: 941

Answers (1)

Kota Mori
Kota Mori

Reputation: 6740

If you don't care about the type stability, you can make Any type array.

ps = Array{Any}(nothing, 3)
ps[1] = plot([2,3,4])
ps[2] = plot([1,5])
ps[3] = plot([10,5,1,0])

@show typeof(ps)
plot(ps..., layout=(3,1))

If you want to create an array of Plot type specifically, one approach is to initialize an array with a dummy plot, then replace later.

ps = repeat([plot(1)], 3)
ps[1] = plot([2,3,4])
ps[2] = plot([1,5])
ps[3] = plot([10,5,1,0])

@show typeof(ps)
plot(ps..., layout=(3,1))

Upvotes: 4

Related Questions