# Colorbar positionning with Makie

**URL:** <https://discourse.julialang.org/t/colorbar-positionning-with-makie/110582>\
**Category:** Visualization\
**Created:** [February 22, 2024, 2:37pm UTC](https://discourse.julialang.org/t/colorbar-positionning-with-makie/110582 "2024-02-22T14:37:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdestouet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdestouet/32/6175_2.png) [@gdestouet](https://discourse.julialang.org/u/gdestouet)\
**Post date:** [February 22, 2024, 2:37pm UTC](https://discourse.julialang.org/t/colorbar-positionning-with-makie/110582/1 "2024-02-22T14:37:02Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [February 22, 2024, 4:03pm UTC](https://discourse.julialang.org/t/colorbar-positionning-with-makie/110582/2 "2024-02-22T16:03:19Z")

</div>

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:

```julia
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](https://global.discourse-cdn.com/julialang/original/3X/9/6/962938b77fbbdb5aaed84409a7f373340b0daece.png)

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

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

```

 ![alternative_2](https://global.discourse-cdn.com/julialang/original/3X/9/d/9d8f47b45aea3a190de67278d0dda977a1fb5ca1.png)

---

<div class="post-metadata">

**Author:** ![gdestouet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdestouet/32/6175_2.png) [@gdestouet](https://discourse.julialang.org/u/gdestouet)\
**Post date:** [February 22, 2024, 4:53pm UTC](https://discourse.julialang.org/t/colorbar-positionning-with-makie/110582/3 "2024-02-22T16:53:34Z")

</div>

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 😅.

---

<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 22, 2024, 5:55pm UTC](https://discourse.julialang.org/t/colorbar-positionning-with-makie/110582/4 "2024-02-22T17:55:36Z")

</div>

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

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

```

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

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.

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

```

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

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