# Move colorbar to the center of figure using Makie

**URL:** <https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [March 28, 2025, 2:23pm UTC](https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456 "2025-03-28T14:23:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dk-zen](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dk-zen](https://discourse.julialang.org/u/dk-zen)\
**Post date:** [March 28, 2025, 2:23pm UTC](https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456/1 "2025-03-28T14:23:23Z")

</div>

Hello everyone! I want to put the colorbar at the center of figure using Makie. How can I get it?  
The result I want just likes this:

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

---

<div class="post-metadata">

**Author:** ![eteppo](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@eteppo](https://discourse.julialang.org/u/eteppo)\
**Post date:** [March 28, 2025, 2:35pm UTC](https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456/2 "2025-03-28T14:35:44Z")

</div>

Hey! Did you try the `axislegend` function like `axislegend(position = :cc)`? [Documentation: Legend | Makie](https://docs.makie.org/dev/reference/blocks/legend#Legend-Inside-An-Axis)

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [March 28, 2025, 2:58pm UTC](https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456/3 "2025-03-28T14:58:17Z")

</div>

This is actually a bit more complicated:

```julia
using WGLMakie, GeometryBasics

f, ax, pl = heatmap(rand(10, 10))

cb = Colorbar(f[1, 1], pl;
        vertical=false, label = "Colorbar in the middle",
        width=200,
        bbox = ax.scene.viewport, tellwidth=false, tellheight=false,
        Makie.legend_position_to_aligns(:cc)...,
)
# Move the whole cbar in front of heatmap
translate!(cb.blockscene, 0, 0, 10)
rect = Rect2f(boundingbox(cb.blockscene))
bg_rect = Rect2f(origin(rect) .- 4, widths(rect) .+ 8)
bg = poly!(cb.blockscene,
    bg_rect, strokewidth=2, space=:pixel, 
    color=:white, strokecolor=:black
)
# Move the background behind cbar
translate!(bg, 0, 0, -1)
f

```

---

<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 28, 2025, 3:12pm UTC](https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456/4 "2025-03-28T15:12:49Z")

</div>

Would be easier to make a nested grid, put a box and a colorbar in it and translate their scenes a bit forward. The grid needs tellwidth and tellheight false

---

<div class="post-metadata">

**Author:** ![dk-zen](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dk-zen](https://discourse.julialang.org/u/dk-zen)\
**Post date:** [March 31, 2025, 12:29pm UTC](https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456/5 "2025-03-31T12:29:24Z")

</div>

Thanks. Your suggestion works. I have got the figure as follows. But I don’t know how to put a box with white backgroudcolor in it. Any ideas?

```julia
# plot
fig = Figure(; size=(800, 800))

ax = Axis(fig[1, 1],
    xlabel="X [EAST->WEST]", ylabel="Y [SOUTH->NORTH]",
    aspect=1) 

cmap = to_colormap(:batlowK)[50:256] 

hm = heatmap!(ax, data[:, :, 1],
    colorrange=(1.0, 2.0),
    colormap=cmap,
    highclip=:white,
    lowclip=:black,
)

# Would be easier to make a nested grid, put a box and a colorbar in it and translate their scenes a bit forward. The grid needs tellwidth and tellheight false
nested_grid = GridLayout()

inset_box = Axis(fig,
    width=Relative(0.3),
    height=Relative(0.2),
    halign=0.5,
    valign=0.5,
    backgroundcolor=:white
)

# Box(inset_box, color=(:cyan, 0.5))

nested_grid[1, 1] = inset_box
fig.layout[1, 1] = nested_grid

cb_nested = Colorbar(nested_grid[1, 1],
    colormap=cmap,
    limits=(1.0, 2.0),
    labelcolor=:yellow,
    tickcolor=:yellow,
    ticklabelcolor=:yellow,
    topspinecolor=:red,
    rightspinecolor=:red,
    leftspinecolor=:red, 
    flipaxis=false,
    flip_vertical_label=false,
    vertical=false,
    height=20,
    tellwidth=false,
    tellheight=false,
    width=Relative(0.2),
    highclip=:white,
    lowclip=:black)

Label(fig[1, 1],
    color=:yellow,
    "Line Width [FHWM: ×0.1nm]",
    halign=0.5,
    valign=0.55,
    tellheight=false,
    tellwidth=false)

translate!(cb_nested.blockscene, 0, 0, 10)
translate!(inset_box.scene, 0, 0, 8)

fig

# save the figure as a PNG file
# save("clwm.png", fig)

```

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

---

<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 31, 2025, 12:39pm UTC](https://discourse.julialang.org/t/move-colorbar-to-the-center-of-figure-using-makie/127456/6 "2025-03-31T12:39:08Z")

</div>

```julia
f = Figure()

ax = Axis(f[1, 1], backgroundcolor = :red)

inner_gl = GridLayout(f[1, 1], tellwidth = false, tellheight = false)
box = Box(inner_gl[1, 1], color = :gray80)
cbar = Colorbar(inner_gl[1, 1], label = "color", width = 50, vertical = false, alignmode = Outside(15, 15, 10, 10))
for x in [box, cbar]
    translate!(x.blockscene, 0, 0, 200)
end
f

```

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