# Makie.jl: Limiting height of Colorbar to that of a nearby equal-aspect axis

**URL:** <https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [February 23, 2021, 6:53pm UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876 "2021-02-23T18:53:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 23, 2021, 6:53pm UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876/1 "2021-02-23T18:53:47Z")

</div>

Hi there,

I have the following code:

```julia
fig, abmstepper = abm_plot(model)
ax, hm = heatmap(fig[1,2], model.sugar_capacities; colormap=cgrad(:thermal))
ax.aspect = AxisAspect(1) # equal aspect ratio for heatmap
Colorbar(fig[1, 3], hm, width = 10, tellheight=false)
fig

```

whose internal details do not matter and the first line can be replaced by `scatter(rand(50,2))`

This code produces this image:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/8/58906360ac670291e15b72b7cf28671340b7b991.png)

As you can see, the Colorbar is too high with respect to the other plots. I quickly realized that `tellheight` doesn’t help here, since of course the Colorbar’s height is decided by the figure available height, not the nearby axis.

How do I make it so that the Colorbar is as high as the axis to its left?

---

<div class="post-metadata">

**Author:** ![Kolaru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kolaru/32/4574_2.png) [@Kolaru](https://discourse.julialang.org/u/Kolaru)\
**Post date:** [February 23, 2021, 7:13pm UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876/2 "2021-02-23T19:13:53Z")

</div>

Setting the height of the row in the layout to your ax size seems to work (may be a bit hacky though)

```julia
using CairoMakie

begin
    fig = Figure(resolution=(400, 800))
    ax = Axis(fig[1, 1])
    hm = heatmap!(ax, rand(100, 100), tellheight=true)
    ax.aspect = DataAspect()
    cb = Colorbar(fig[1, 2], hm, width = 10, tellheight=true)
    rowsize!(fig.layout, 1, ax.scene.px_area[].widths[2])
    fig
end

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/9/89ab849d9c0bb1668edefc3b654b2c1b323a4a0f.jpeg)

---

<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:** [February 23, 2021, 7:15pm UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876/3 "2021-02-23T19:15:25Z")

</div>

Aspects are always a bit tricky, because MakieLayout is not a constraint solver, but has an algorithm that tries to calculate row and column sizes given the information it can collect about the layouts and their entries. If the axes decide they are smaller than the space they get, that’s their prerogative but the layout doesn’t know.

My first instinct is this, although it’s not perfect as I would like the resulting row to be centered, which it isn’t because the layout algorithm can’t infer the row height:

You set the row-height to Aspect(2, 1), so aspect of 1 with column 2, where your axis is:

```julia
f = Figure()
Axis(f[1, 1])
Axis(f[1, 2])
Colorbar(f[1, 3], width = 30)
rowsize!(f.layout, 1, Aspect(2, 1))
f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/a/9/a927b5de20cb65ba5bff7afe10080067b152f49f.png)

I’ve made the background gray so you can see the problematic top alignment, I’ll think some more about this

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 23, 2021, 7:20pm UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876/4 "2021-02-23T19:20:35Z")

</div>

Thanks guys. Although @Kolaru 's answer seems more complex coding-wise, it looks better than having everyting in the top

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [February 23, 2021, 8:24pm UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876/5 "2021-02-23T20:24:52Z")

</div>

I always do `height = Relative(2/4)` or `height = Relative(3/4)`. They usually work quite well, not perfectly, but good enough 😃

---

<div class="post-metadata">

**Author:** ![Kolaru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kolaru/32/4574_2.png) [@Kolaru](https://discourse.julialang.org/u/Kolaru)\
**Post date:** [February 23, 2021, 9:31pm UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876/6 "2021-02-23T21:31:13Z")

</div>

Thinking about it, I think that it would be cool if setting `ax.tellheight = true` on the axis would coerce the row to its size, the same way setting it for `Label` does.

---

<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:** [February 25, 2021, 6:57am UTC](https://discourse.julialang.org/t/makie-jl-limiting-height-of-colorbar-to-that-of-a-nearby-equal-aspect-axis/55876/7 "2021-02-25T06:57:52Z")

</div>

The problem is that the axis size in that case depends on the layout cell it is provided, tellheight only works if the object knows its size before a cell is provided. Otherwise you have circular logic 🙂 A constraint solver could maybe deal with this better, but I tried using one in the beginning and it was just very hard to debug whenever something weird happened with the layout.
