Colorbar positionning with Makie

Hi everyone,

I am currently struggling to make a colorbar stick to the right yaxis of a contour plot whose x axis need to be align with an other line plot just below.

fig = Figure()
ax1 = Axis(fig[1,1])
Box(fig[1, 1], color = (:red, 0.2), strokewidth = 0)
ax2 = Axis(fig[2,1])
ax3 = Axis(fig[2,1],yaxisposition=:right)
cb = Colorbar(fig[1,2]))
colgap!(fig.layout,0)
rowgap!(fig.layout,0)
hidexdecorations!(ax1)
fig

On the lower plot I would like to have two y axis on the left and right side but making a right y axis (switching yaxisposition to :right for ax4 ) creates a gap between the colorbar and the contour plot (which I would like to remove to save space !). And colgap!(fig.layout,0) doesn’t seem to do doing anything in that case.

If you have any ideas on the subject, I’d be grateful!

Thanks in advance

The layout takes the right axis y ticks into account when setting the gap to zero, so there really is no gap between the columns (accounting for the right y-ticklabels). I’m not sure how to make the layout algorithm ignore anything that isn’t a “box”, but you can hack it by setting a negative colgap:

using GLMakie
fig = Figure()
ax1 = Axis(fig[1,1])
Box(fig[1, 1], color = (:red, 0.2), strokewidth = 0)
ax2 = Axis(fig[2,1])
ax3 = Axis(fig[2,1],yaxisposition=:right)
cb = Colorbar(fig[1,2])
colgap!(fig.layout,-24)
rowgap!(fig.layout,0)
hidexdecorations!(ax1)
fig

alternative_1

If you want to save space though, another alternative would be to include the top colorbar in the space of the top box:

fig = Figure()
gl1 = GridLayout(fig[1,1])
gl2 = GridLayout(fig[2,1])

ax1 = Axis(gl1[1,1])
Box(gl1[1, 1], color = (:red, 0.2), strokewidth = 0)
ax2 = Axis(gl2[1,1])
ax3 = Axis(gl2[1,1],yaxisposition=:right)
cb = Colorbar(gl1[1,2])
colgap!(gl1, 0)
rowgap!(fig.layout, 0)
hidexdecorations!(ax1)
fig

1 Like

Thank you very much !

I am going for the negative colgap way. Both x axes of the contour and line plots need to be linked and have the same length.

I should have tried negative values before but I didn’t dare I guess :sweat_smile:.

Two canonical ways to do this: Either you set the alignmode of the lower axis such that it pretends its right protrusion is zero:

fig = Figure()
ax1 = Axis(fig[1,1])
Box(fig[1, 1], color = (:red, 0.2), strokewidth = 0)
ax2 = Axis(fig[2,1])
ax3 = Axis(fig[2,1],yaxisposition=:right, alignmode = Mixed(right = Makie.Protrusion(0)))
cb = Colorbar(fig[1,2])
colgap!(fig.layout,0)
rowgap!(fig.layout,0)
hidexdecorations!(ax1)
fig

Or you put the Colorbar not in a new layout column but into the right protrusion of cell 1, 1. You also need to give it alignmode = Outside() in that case or it won’t reserve space for its own right protrusion.

fig = Figure()
ax1 = Axis(fig[1,1])
Box(fig[1, 1], color = (:red, 0.2), strokewidth = 0)
ax2 = Axis(fig[2,1])
ax3 = Axis(fig[2,1],yaxisposition=:right)
cb = Colorbar(fig[1,1,Right()], alignmode = Outside())
colgap!(fig.layout,0)
rowgap!(fig.layout,0)
hidexdecorations!(ax1)
fig

The second option is probably better because the lower ticklabels could be super long, and then you wouldn’t want to ignore them.

2 Likes