CairoMakie ylabel allignment not working

Hello,

I’m new to Makie (used to use Plots before). I’ve been trying to align the ylabels for many subplots using the CairoMakie package. I’m able to align them, but for some reason there is some overlap with the tick marks of the plot… Here is my minimal working example:

f = Figure()

ax1 = Axis(f[1, 1], title = "Axis 1", ylabel = "y label", ytickformat = "{:.3f}")
ax2 = Axis(f[2, 1], title = "Axis 2", ylabel = "y label", xlabel = "x label")
ax3 = Axis(f[2, 2], title = "Axis 3", xlabel = "x label", xtickformat = "{:.3f}", xticklabelrotation = pi/4)

# Here is my addition to the code in the Makie tutorial
lines!(ax1, rand(length(times))*1e5)
lines!(ax2, rand(length(times))*1e2)

yspace = maximum(tight_yticklabel_spacing!, [ax1, ax2])
xspace = maximum(tight_xticklabel_spacing!, [ax2, ax3])

ax1.yticklabelspace = yspace
ax2.yticklabelspace = yspace

ax2.xticklabelspace = xspace
ax3.xticklabelspace = xspace

f

This yields this:

The code comes from this makie tutorial which works fine before I add the lines to the plot. As you can see, the label is hidden by the tick labels.

Am I doing something wrong here? Sorry if this is a newbie question :slight_smile:

Thanks a lot!

Ah that’s tricky :slight_smile: The reason is that these days, the limits of an Axis don’t immediately update when a plot is added. That’s a performance optimization.

So when you measure the tick spacing, it’s not for the limits you see in the final plot, it’s for the initial limits. That’s why they’re too small.

To adjust the limits early, call reset_limits!(ax) on each axis.

1 Like

Amazing! Thank you, that works!

For other people in a similar position, here is the fix in context:

f = Figure()
	
ax1 = Axis(f[1, 1], title = "Axis 1", ylabel = "y label", ytickformat = "{:.3f}")
ax2 = Axis(f[2, 1], title = "Axis 2", ylabel = "y label", xlabel = "x label")
ax3 = Axis(f[2, 2], title = "Axis 3", xlabel = "x label", xtickformat = "{:.3f}", xticklabelrotation = pi/4)
	
# Here is my addition to the code in the Makie tutorial
times = 0:1000
lines!(ax1, rand(length(times))*1e5)
lines!(ax2, rand(length(times))*1e2)

# Reset limits
reset_limits!(ax1)
reset_limits!(ax2)
reset_limits!(ax3)
	
yspace = maximum(tight_yticklabel_spacing!, [ax1, ax2])
xspace = maximum(tight_xticklabel_spacing!, [ax2, ax3])
	
ax1.yticklabelspace = yspace
ax2.yticklabelspace = yspace
	
ax2.xticklabelspace = xspace
ax3.xticklabelspace = xspace
	
f

(also note a typo in the original question: I forgot to define the variable time)

1 Like