# MakieLayout - Alignment of horizontal colorbar and legend

**URL:** <https://discourse.julialang.org/t/makielayout-alignment-of-horizontal-colorbar-and-legend/57804>\
**Category:** Visualization\
**Tags:** makie\
**Created:** [March 23, 2021, 5:36pm UTC](https://discourse.julialang.org/t/makielayout-alignment-of-horizontal-colorbar-and-legend/57804 "2021-03-23T17:36:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabgrei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fabgrei/32/4214_2.png) [@fabgrei](https://discourse.julialang.org/u/fabgrei)\
**Post date:** [March 23, 2021, 5:36pm UTC](https://discourse.julialang.org/t/makielayout-alignment-of-horizontal-colorbar-and-legend/57804/1 "2021-03-23T17:36:40Z")

</div>

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

```julia
horizontal_colorbar(nothing, nothing)

```

![image](https://global.discourse-cdn.com/julialang/original/3X/7/6/76ec48bc7142662376bc66157a4decf03a990acf.png)

```julia
horizontal_colorbar("label", "other label")

```

![image](https://global.discourse-cdn.com/julialang/original/3X/b/6/b69ec540886a720c0992c9040450b0b88b95d41c.png)

Here’s the code

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

```

---

<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:** [March 23, 2021, 5:49pm UTC](https://discourse.julialang.org/t/makielayout-alignment-of-horizontal-colorbar-and-legend/57804/2 "2021-03-23T17:49:01Z")

</div>

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.

---

<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:** [March 23, 2021, 5:54pm UTC](https://discourse.julialang.org/t/makielayout-alignment-of-horizontal-colorbar-and-legend/57804/3 "2021-03-23T17:54:56Z")

</div>

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