# Plots.jl: Side-by-side subplots

**URL:** <https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469>\
**Category:** Visualization\
**Tags:** plots\
**Created:** [May 3, 2021, 3:40pm UTC](https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469 "2021-05-03T15:40:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [May 3, 2021, 3:40pm UTC](https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469/1 "2021-05-03T15:40:50Z")

</div>

Hi,  
I want make 2 subplots with 2 function plots each in a 1-row 2-columns format. Below is a minimum working example for my use case.

```julia
x = -2π:0.1:+2π
y11 = sin.(x)
y12 = sin.(x .+ 0.25π)
y21 = cos.(x)
y22 = sin.(x) .* cos.(x)

plot(
    plot(x, y11),
    plot!(x, y12),
    
    plot(x, y21),
    plot!(x, y22)
)

```

The result I get is with plots vertically stacked (2-row 1-column layout) as below.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/1/9/19c20c17800bc09e4a35caa0af9e63c14cb04efd.png)

How can I fix this to get subplots side-by-side?

With thanks and regards,  
Vishnu Raj

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [May 3, 2021, 3:47pm UTC](https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469/2 "2021-05-03T15:47:44Z")

</div>

```julia
p=plot(x, y11,layout=(1,2))
plot!(p,x, y12)
plot!(p,x, y21, subplot=2)
plot!(p,x, y22, subplot=2)

```

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [May 3, 2021, 3:58pm UTC](https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469/3 "2021-05-03T15:58:57Z")

</div>

Other options:  
Constructing two plots and passing them to `plot`:

```julia
plt1 = plot(x, y11)
plot!(plt1, x, y12)
plt2 = plot(x, y21)
plot!(plt2, x, y22)
plot(plt1, plt2)

```

Using columns to make separate series:

```julia
plot(plot([x x], [y11 y12]),
     plot([x x], [y21 y22]))

```

A one-liner similar to your original code (but this is less readable, I think):

```julia
plot(plot!(plot(x, y11), x, y12), 
     plot!(plot(x, y21), x, y22)
    )

```

---

<div class="post-metadata">

**Author:** ![v-i-s-h](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/v-i-s-h/32/46152_2.png) [@v-i-s-h](https://discourse.julialang.org/u/v-i-s-h)\
**Post date:** [May 3, 2021, 5:26pm UTC](https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469/4 "2021-05-03T17:26:21Z")

</div>

Hi @yha, Thanks for the snippets. I really liked the last oneliner you gave 🙂

I tried to modify that and came up with version below

```julia
plot(
    plot(x, y11) |> p -> plot!(p, x, y12),
    plot(x, y21) |> p -> plot!(p, x, y22)
)

```

Still not easily readable, but somehow shows julia’s beauty.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 3, 2021, 6:03pm UTC](https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469/5 "2021-05-03T18:03:24Z")

</div>

I’d do

```julia
plot(plot([sin, x -> sin(x + 0.25π)], x),
     plot([cos, x -> sin(x)*cos(x)], x), 
     layout = (1, 2))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 3, 2021, 7:17pm UTC](https://discourse.julialang.org/t/plots-jl-side-by-side-subplots/60469/6 "2021-05-03T19:17:57Z")

</div>

Simpler:

```julia
plot( plot(x, [y11, y12]), plot(x, [y21,y22]) )

```

![Plots_gr_1x2_layout](https://global.discourse-cdn.com/julialang/original/3X/7/4/743634098143ff25e9e9257884bbb139d55f5d62.png)
