Idiomatic way to overplot on subplots

If I want to create 10 histograms of 10 samples, and overplot a predicted curve on each one, and then plot all 10 subplots in a layout, what’s the most Julianic way of doing this? I’ve been using nested loops and a collector, then plot(), but it seems that there must be an idiomatic way to say, for instance, plot!(data, overplot_on=histogram1).

Instead of relying on nested loops:

using Plots
using Distributions

n = 10                                      
data = [randn(100) .+ i for i in 1:n] 
x = -5:0.1:15
curves = [pdf.(Normal(i, 1), x) for i in 1:n]

lay = @layout [grid(2, 5)]

plt = plot(layout=lay, size=(1000, 500))

for i in 1:n
    histogram!(data[i], bins=20, label="", subplot=i)
    plot!(x, curves[i], label="Predicted", subplot=i)
end

display(plt)
1 Like

Nope.

Totally works great, and so much clearer to program and to read. Thanks!

1 Like