# Idiomatic way to overplot on subplots

**URL:** <https://discourse.julialang.org/t/idiomatic-way-to-overplot-on-subplots/123844>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [December 14, 2024, 6:47pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-overplot-on-subplots/123844 "2024-12-14T18:47:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mekapses](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mekapses](https://discourse.julialang.org/u/mekapses)\
**Post date:** [December 14, 2024, 6:47pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-overplot-on-subplots/123844/1 "2024-12-14T18:47:01Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Fe-r-oz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fe-r-oz/32/216415_2.png) [@Fe-r-oz](https://discourse.julialang.org/u/Fe-r-oz)\
**Post date:** [December 14, 2024, 7:43pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-overplot-on-subplots/123844/2 "2024-12-14T19:43:26Z")

</div>

Instead of relying on nested loops:

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![Fe-r-oz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fe-r-oz/32/216415_2.png) [@Fe-r-oz](https://discourse.julialang.org/u/Fe-r-oz)\
**Post date:** [December 14, 2024, 9:17pm UTC](https://discourse.julialang.org/t/idiomatic-way-to-overplot-on-subplots/123844/4 "2024-12-14T21:17:41Z")

</div>

Nope.

---

<div class="post-metadata">

**Author:** ![mekapses](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mekapses](https://discourse.julialang.org/u/mekapses)\
**Post date:** [December 16, 2024, 12:01am UTC](https://discourse.julialang.org/t/idiomatic-way-to-overplot-on-subplots/123844/5 "2024-12-16T00:01:30Z")

</div>

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