# Plot images within a loop

**URL:** <https://discourse.julialang.org/t/plot-images-within-a-loop/97960>\
**Category:** General Usage\
**Tags:** plots\
**Created:** [April 26, 2023, 5:49pm UTC](https://discourse.julialang.org/t/plot-images-within-a-loop/97960 "2023-04-26T17:49:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jianqixi](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@jianqixi](https://discourse.julialang.org/u/jianqixi)\
**Post date:** [April 26, 2023, 5:49pm UTC](https://discourse.julialang.org/t/plot-images-within-a-loop/97960/1 "2023-04-26T17:49:58Z")

</div>

I am trying to plot some data within one loop using the following code:

n=length(B)  
for i in 1:n  
i=contourf(x,y,B[i], layout=n)  
display(i)  
end

Here B is a matrix and each B[i] is one array. When using the above code, it only output one image and some others are overlapped, as shown below. I am wondering how could I plot all the images as a function of i. Thanks  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/d/3d96545d2cd98fa05159a93ca44c0964f22d3e7e.png)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [April 26, 2023, 5:53pm UTC](https://discourse.julialang.org/t/plot-images-within-a-loop/97960/2 "2023-04-26T17:53:41Z")

</div>

Maybe try the following?

```julia
julia> ps = map(1:4) do s
           x = rand(100)
           y = s * x .+ rand(100)
           scatter(x, y, ylim = [0, 5])
       end
4-element Vector{Plots.Plot{Plots.GRBackend}}:
 Plot{Plots.GRBackend() n=1}
 Plot{Plots.GRBackend() n=1}
 Plot{Plots.GRBackend() n=1}
 Plot{Plots.GRBackend() n=1}

julia> plot(ps...)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 26, 2023, 5:56pm UTC](https://discourse.julialang.org/t/plot-images-within-a-loop/97960/3 "2023-04-26T17:56:29Z")

</div>

or:

```julia
julia> using Plots

julia> plt = plot(layout=(2,3)) # create plot object with the desired layout

julia> for i in 1:6
           plot!(plt, rand(10), subplot=i) # plot each set in a different subplot
       end

julia> display(plt)

```

---

<div class="post-metadata">

**Author:** ![jianqixi](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@jianqixi](https://discourse.julialang.org/u/jianqixi)\
**Post date:** [April 26, 2023, 6:05pm UTC](https://discourse.julialang.org/t/plot-images-within-a-loop/97960/4 "2023-04-26T18:05:00Z")

</div>

I tried using above way, but no data was plotted, maybe I had missed something. Here is my code:

n=length(B)  
plt=plot(layout=n)  
for i in 1:n  
plot!(plt, contourf(xs,ys,B[i]), subplot=i)  
end  
display(plt)

![image](https://global.discourse-cdn.com/julialang/original/3X/c/1/c1a4508c88c2df70c96e02cc67e45b5508089eaa.png)

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [April 26, 2023, 6:07pm UTC](https://discourse.julialang.org/t/plot-images-within-a-loop/97960/5 "2023-04-26T18:07:02Z")

</div>

> [@jianqixi](#):
>
> plot!(plt, contourf(xs,ys,B[i]), subplot=i)

this should be just

```julia
contourf!(plt, xs, ys, B[i], subplot=i)

```

the `contourf!` replaces the `plot!`call, which is for plotting lines.

---

<div class="post-metadata">

**Author:** ![jianqixi](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@jianqixi](https://discourse.julialang.org/u/jianqixi)\
**Post date:** [April 26, 2023, 6:13pm UTC](https://discourse.julialang.org/t/plot-images-within-a-loop/97960/6 "2023-04-26T18:13:28Z")

</div>

I see, now it works. Thank you!
