Plots.jl: Side-by-side subplots

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.

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

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

With thanks and regards,
Vishnu Raj

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

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

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:

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

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

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

I tried to modify that and came up with version below

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.

1 Like

I’d do

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

Simpler:

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

Plots_gr_1x2_layout

5 Likes