Move colorbar to the center of figure using Makie

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:

Hey! Did you try the axislegend function like axislegend(position = :cc)? Documentation: Legend | Makie

This is actually a bit more complicated:

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
1 Like

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

1 Like

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?

# 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)

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