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

**URL:** <https://discourse.julialang.org/t/how-to-set-the-axis-padding-precisely-in-makie-jl/121310>\
**Category:** General Usage\
**Tags:** makie\
**Created:** [October 15, 2024, 3:38am UTC](https://discourse.julialang.org/t/how-to-set-the-axis-padding-precisely-in-makie-jl/121310 "2024-10-15T03:38:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lee123](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@Lee123](https://discourse.julialang.org/u/Lee123)\
**Post date:** [October 15, 2024, 3:38am UTC](https://discourse.julialang.org/t/how-to-set-the-axis-padding-precisely-in-makie-jl/121310/1 "2024-10-15T03:38:59Z")

</div>

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

 ![截屏2024-10-15 11.21.10](https://global.discourse-cdn.com/julialang/original/3X/8/9/892fcdc587ace5e90573efa992cc6bce5e9c59c6.png)  
I have try to set `figure_padding`, `halign`, `valign`, but they doesn’t work

```julia
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.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 15, 2024, 7:46am UTC](https://discourse.julialang.org/t/how-to-set-the-axis-padding-precisely-in-makie-jl/121310/2 "2024-10-15T07:46:29Z")

</div>

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):

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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac908d9bad8d48f5ae5279558256422ccdae7a62.png)

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:

```julia
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:

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2238cb6febaab17bae41ca913c0e2beebd27746.jpeg)

---

<div class="post-metadata">

**Author:** ![Lee123](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@Lee123](https://discourse.julialang.org/u/Lee123)\
**Post date:** [October 15, 2024, 10:54am UTC](https://discourse.julialang.org/t/how-to-set-the-axis-padding-precisely-in-makie-jl/121310/3 "2024-10-15T10:54:09Z")

</div>

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

 ![截屏2024-10-15 18.48.40](https://global.discourse-cdn.com/julialang/original/3X/9/0/90aadd7e4fcb1467201843a956f514ea1ff12445.png)  
problem solved, thanks a lot!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 15, 2024, 11:21am UTC](https://discourse.julialang.org/t/how-to-set-the-axis-padding-precisely-in-makie-jl/121310/4 "2024-10-15T11:21:10Z")

</div>

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](https://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)`.

---

<div class="post-metadata">

**Author:** ![Lee123](https://avatars.discourse-cdn.com/v4/letter/l/43a26b/32.png) [@Lee123](https://discourse.julialang.org/u/Lee123)\
**Post date:** [October 15, 2024, 11:43am UTC](https://discourse.julialang.org/t/how-to-set-the-axis-padding-precisely-in-makie-jl/121310/5 "2024-10-15T11:43:31Z")

</div>

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.
