How to use @layout in plots?

Hello,

I am trying to create layouts for subplots, which are not pure grids. However, I find very little instructions on this. What I have found is a small section in the docs (Layouts · Plots which also contains a broken link to the Julia’s multidimensional Array construction multidimensional array construction. I think I found what it is supposed to be pointing at, but it would be helpful to be sure). What was most useful might have been this very short discussion on the discourse: Plots.jl @layout macro usage

There is the macro for creating layouts, with and example:

l = @layout [
    a{0.3w} [grid(3,3)
             b{0.2h}  ]
]
plot(
    rand(10, 11),
    layout = l, legend = false, seriestype = [:bar :scatter :path],
    title = ["($i)" for j in 1:1, i in 1:11], titleloc = :right, titlefont = font(8)
)

But say I want to remove either the plot on the bottom, or the top. It feels intuitive that I would simply do some version of

l = @layout [
             [grid(3,3)
             b{0.2h}  ]
]

for removing the one on the left

or

l = @layout [
    a{0.3w} [grid(3,3)
               ]
]

for removing the one on the bottom. But both these yields errors.

Is there some more extensive instructions somewhere which I have missed, or is the documentations simply not complete?
(or maybe most likely, is this the documentation for the macro, and I simply not able to understand it?)

Ideally, I would like to create something like a 3x3 grid of plots, and next to it a 1x4 of 4 plots (of identical width as the 3x3 plots, with the top and bottom plot at half the hight of the plots in the 3x3, and the two middle ones of identical height)

1 Like

If you find any better explanation of that, please post it here. I recently wanted to build a complex layout with plots, and got completely frustrated.

the outer array now has only a single element, making it a vector, which is confusing to layouts because it expects things that make a matrix. You just just need to drop one pair of brackets for it to work:

l = @layout [grid(3,3)
             b{0.2h}  ]

Similar with the other one, drop the unused brackets.
What you want for your 4 in a row + 3 grid thing could be:

julia> l = @layout [[a{0.25h};b;c;d{0.25h}] grid(3,3)]
Plots.GridLayout(1, 2)

EDIT: Half of 1/4 is 0.125 not 0.25 ofc, that one’s on me. Here’s a working example:

julia> l = @layout [[a{0.125h};b;c;d{0.125h}] grid(3,3){0.66w}]
Plots.GridLayout(1, 2)

julia> plot(
           rand(10, 13),
           layout = l, legend = false, seriestype = [:bar :scatter :path],
           title = ["($i)" for j in 1:1, i in 1:13], titleloc = :right, titlefont = font(8)
           )
1 Like

Thanks, that is really helpful! I managed to create the layout I wanted using your code. It also helped me understand how it worked better, so I managed to create some other designs which I wanted to have! Loads of thanks!

2 Likes