# Add to subplots within a loop using Plots.jl?

**URL:** <https://discourse.julialang.org/t/add-to-subplots-within-a-loop-using-plots-jl/57739>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [March 22, 2021, 6:26pm UTC](https://discourse.julialang.org/t/add-to-subplots-within-a-loop-using-plots-jl/57739 "2021-03-22T18:26:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [March 22, 2021, 6:26pm UTC](https://discourse.julialang.org/t/add-to-subplots-within-a-loop-using-plots-jl/57739/1 "2021-03-22T18:26:49Z")

</div>

I am trying to plot some data I have and am having trouble with using subplots. The way things are structured, I need to be able to add data to each subplot incrementally within a loop, then combine into a final plot afterwards. Here is my MNE (minimum non-working example):

```julia
x = 0:0.1:1
p1 = plot()
p2 = plot()
p3 = plot()
for i = 1:3
    p1 = scatter!(x, x.+i, leg=false)
    p2 = scatter!(x, x.*i, leg=false)
    p3 = scatter!(x, x.^(i), leg=false)
end
plot(p1, p2, p3, layout=(3, 1))

```

Doing it this way, all the data seems to get pushed into `p1` leaving `p2` and `p3` blank. Is there a way to get this to work?

edit: The goal is to have something that looks similar to this:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/4/746ac042d8f2aefa6fe636d4b697f6e307f9c9d8.png)

---

<div class="post-metadata">

**Author:** ![Oto\_Brzobohaty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oto_brzobohaty/32/10370_2.png) [@Oto\_Brzobohaty](https://discourse.julialang.org/u/Oto_Brzobohaty)\
**Post date:** [March 22, 2021, 6:34pm UTC](https://discourse.julialang.org/t/add-to-subplots-within-a-loop-using-plots-jl/57739/2 "2021-03-22T18:34:45Z")

</div>

```julia
for i = 1:3
    scatter!(p1, x, x.+i, leg=false)
    scatter!(p2,x, x.*i, leg=false)
    scatter!(p3, x, x.^(i), leg=false)
end

```

---

<div class="post-metadata">

**Author:** ![mihalybaci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mihalybaci/32/13528_2.png) [@mihalybaci](https://discourse.julialang.org/u/mihalybaci)\
**Post date:** [March 22, 2021, 6:36pm UTC](https://discourse.julialang.org/t/add-to-subplots-within-a-loop-using-plots-jl/57739/3 "2021-03-22T18:36:57Z")

</div>

Well, that was easy. Thanks!
