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:

Is there also an idea how to align the tick-spacing to be equidistant?

What do you mean? Depending on axis limits, it depends what “good” ticks are and if those can be aligned across axes. If you know ticks which will do that you can set them manually.

1 Like

Thanks for your immediate replay! - Equidistant means if xaxis_dtick is 0.1 yaxis_dtick should be 0.1 as well (dtick is the notation for plotly/PlotlyJS).
Ok, understood, there is the option to define, ticks and tick-labels manually, but I am looking for something more convenient.

Aha so you want automatic ticks in a certain distance? It would not be hard to write a tick finder for that yourself (you just have to pick a good value manually)

That could actually be a good addition to Makie’s ticks.

struct DeltaTicks
    delta::Float64
    offset::Float64
end
DeltaTicks(delta) = DeltaTicks(delta, 0.0)

function Makie.get_tickvalues(d::DeltaTicks, ::Any, vmin, vmax)
    i = ceil((vmin - d.offset) / d.delta)
    start = d.offset + i * d.delta
    collect(start:d.delta:vmax)
end

lines(cumsum(randn(10) .* 3), axis = (
    xticks = DeltaTicks(1),
    yticks = DeltaTicks(1),
))

1 Like

@jules
The trick with function Makie.get_tickvalues(d::DeltaTicks, ::Any, vmin, vmax) [...] end is amazing,
it took a while to understand your code. Thanks a lot!!!

Here is the result, containing your contribution:
InteractiveEquivalentCircuit