MakieLayout - Alignment of horizontal colorbar and legend

I would like to have the colorbar and the legend nicely aligned. Is there a way to perfectly align the top row of the legend with tick labels of the colorbar, and also the titles of legend and colorbar?

This is how it looks now

horizontal_colorbar(nothing, nothing)

image

horizontal_colorbar("label", "other label")

image

Here’s the code

function horizontal_colorbar(legend_title, colorbar_title)

	fig = Figure()

	ax = Axis(fig[2,1])
	
	# Legend
	markersizes = [5, 10, 15, 20]

	group_size = [MarkerElement(marker = :circle, color = :black, strokecolor = :transparent,
    	markersize = ms) for ms in markersizes]

	legend = Legend(fig,
    	group_size,
    	string.(markersizes),
    	legend_title,
		nbanks = 2, orientation = :horizontal, tellwidth = false, tellheight = true)

	fig[1, 1][1,1] = legend

	# Colorbar
	cb_attributes = (height = 18, width = Relative(0.7),
		vertical = false,
		valign = :center,
		ticksize = 5,
  		ticklabelpad = 1.5)
	
	cb = Colorbar(fig, limits=(-1, 1); cb_attributes...)

	fig[1,1][1, 2][1,1] = cb
	
	if !isnothing(legend_title)
		Label(fig[1,1][1, 2][0, 1], "legend title", tellheight = true, tellwidth = false)
	end
	
	fig
end

Hmm manually, yes of course. Automatically, working across all legends, colorbars, fonts, fontsizes, etc. not so much. You could measure stuff of course and set the values accordingly. But I’m not sure if you want to do that. For example the colorbar label is at colorbar height + ticklabelspace + labelpadding or something to that effect.

1 Like

You could also deconstruct the legend and put a label in a gridlayout as a title, and the same for the colorbar

1 Like