How can I achieve this Makie Layout that respects a given aspect ratio?

So as you’ve noticed colsize! or rowsize! with Aspect has the drawback that it only works if you know which dimension is the longer/shorter one. Otherwise it can cause overlaps. It doesn’t matter for static plots but for interactive ones with unknown figure size it’s a bit impractical.

GridLayoutBase is not a constraint solver, it only follows one fixed algorithm to determine the outcome, that’s why you can’t have it pick the “best” solution or so. But there are still some tricks, for example in your case you can place one GridLayout where you placed the red box. You only need that for its suggestedbbox observable which updates with the grid cell rectangle basically. From that you can compute a square box and pass that as the manual bbox keyword to another GridLayout. I also assign parent here so that Box afterwards knows which Figure the grid belongs to.

using GLMakie

fig = Figure()
layout_right = fig[1,2] = GridLayout(2,1)
layout_aspect_1 = layout_right[1,1] = GridLayout(1,1)

Box(fig[1,1], color = :green, width = 200)
# Box(layout_aspect_1[1,1], color = :red)
Box(layout_right[2,1], color = :blue)

proxy_layout = GridLayout(layout_aspect_1[1, 1])
bbox = lift(proxy_layout.layoutobservables.suggestedbbox) do bb
    w = minimum(widths(bb))
    diff = widths(bb) .- w
    Rect2f(bb.origin .+ diff ./ 2, (w, w))
end

layout_in_square = GridLayout(bbox = bbox, parent = fig)
Box(layout_in_square[1, 1], color = :red)

rowsize!(layout_right, 2, Auto(0.25))

fig

2 Likes