How to set the axis padding precisely in Makie.jl?

I want to set the padding between axis and figure margin precisely, for example, all padding are 1cm as following


I have try to set figure_padding, halign, valign, but they doesn’t work

cm = 28.3465
x = range(-pi,pi,128)
f = Figure(size=(10,8).*cm, figure_padding=(1,1,1,1).*cm, backgroundcolor=:gray80)
ax = Axis(f[1,1], height=6cm, width=8cm, halign=0cm, valign=1cm)
lines!(x,sin.(x))
f

Thanks in advance for any suggestions.

It’s relatively unusual to want to set the padding between axis and figure border without taking the actual ticklabel sizes into account, as they could either clip outside or leave large whitespace. So you have to work around the fact that Makie wants to include those spaces.

Two variants come to mind, one is to place the axis outside the layout and just specify it’s bbox argument as the figure size minus the padding (by default, the axis will align its inner area to the bbox rectangle):

padding = 50
w, h = (600, 450)
f = Figure(size = (w, h), backgroundcolor = :gray)
Axis(f, bbox = BBox(padding, w-padding, padding, h-padding))
f

Another option is to use the layout mechanism and set the padding you want as the figure_padding but at the same time overwriting all protrusions the Axis reports with 0:

f = Figure(figure_padding = 50, backgroundcolor = :gray)
Axis(
    f[1, 1],
    alignmode = Mixed(
        left = Makie.Protrusion(0),
        right = Makie.Protrusion(0),
        bottom = Makie.Protrusion(0),
        top = Makie.Protrusion(0),
    )
)
f

You can also do this with a parent layout if you want to add more elements to the figure but keep the overall padding. This way you don’t have to set alignmodes on all the individual elements:

f = Figure(figure_padding = 50, backgroundcolor = :gray)
gl = GridLayout(
    f[1, 1],
    alignmode = Mixed(
        left = Makie.Protrusion(0),
        right = Makie.Protrusion(0),
        bottom = Makie.Protrusion(0),
        top = Makie.Protrusion(0),
    )
)
for i in 1:3, j in 1:2
    Axis(gl[i, j])
end
Colorbar(gl[:, end+1])
f

5 Likes

I just want to align two independent figures, e.g.,


problem solved, thanks a lot!

Aha I see, yes for that you can go fully manual, or you just set the one value that matters for alignment here, which is the yticklabelspace. The logic is similar to docs.makie.org/stable/reference/blocks/axis#Aligning-neighboring-axis-labels where it’s done to align the axis labels of two adjacent axes within the same figure. You either set the value by hand so it’s large enough for both, or you actually create the two figures, then do tight_yticklabel_spacing!(ax) on both axes, and then set both ax.yticklabelspace = max(val1, val2).

got it! I come from Mathematica’s plot recently and not familiar with Makie’s logic. I really need to spent some time to read more about the manual.