Optimal layouting with DataAspect()

I’m currently trying to create multipanel plots that all use DataAspect()
and I’ve been wondering if there’s a clever way to generate the optimal layout.
Here’s an example:
The panels are created such that all data limits have the same “height” and the second column is three times wider than the first.

fig = Figure(resolution=(700,400));
ax1 = Axis(fig[1,1], aspect=DataAspect(), limits=(0,1,0,1))
ax2 = Axis(fig[1,2], aspect=DataAspect(), limits=(0,3,0,1))
ax3 = Axis(fig[2,1], aspect=DataAspect(), limits=(0,1,0,1))
ax4 = Axis(fig[2,2], aspect=DataAspect(), limits=(0,3,0,1))
Box.([fig[1,1],fig[1,2], fig[2,1], fig[2,2]])
fig

I’m aware that I can use colsize! but then I still have to manually tune the figure height
to minimize unused space.

fig = Figure(resolution=(700,419));
ax1 = Axis(fig[1,1], aspect=DataAspect(), limits=(0,1,0,1))
ax2 = Axis(fig[1,2], aspect=DataAspect(), limits=(0,3,0,1))
ax3 = Axis(fig[2,1], aspect=DataAspect(), limits=(0,1,0,1))
ax4 = Axis(fig[2,2], aspect=DataAspect(), limits=(0,3,0,1))

Box.([fig[1,1],fig[1,2], fig[2,1], fig[2,2]])
colsize!(fig.layout, 1, Relative(0.25))
fig

You can set the colsizes to Aspect which will result in an “overflowing” gridlayout if the figure doesn’t happen to have the perfect size. Then you resize the figure with the new function resize_to_layout!.

I’ve set the size here to the deliberately unfitting value (400, 400).

fig = Figure(resolution=(400,400), backgroundcolor = :gray80);

ax1 = Axis(fig[1,1], limits=(0,1,0,1))
ax2 = Axis(fig[1,2], limits=(0,3,0,1))
ax3 = Axis(fig[2,1], limits=(0,1,0,1))
ax4 = Axis(fig[2,2], limits=(0,3,0,1))

colsize!(fig.layout, 1, Aspect(1, 1.0))
colsize!(fig.layout, 2, Aspect(1, 3.0))

resize_to_layout!(fig)

fig

In this case, it gets resized to size (705, 400), so you were really close already with (700, 400) for your case.

This wouldn’t work with aspect = DataAspect(), because that just makes the axis itself smaller in its allotted space, but it doesn’t force the layout to fix that cell to an aspect ratio. That has to be done with colsize! or rowsize! on the layout level.

1 Like

That works, thanks a lot! :partying_face: