Manual colorbar location with Makie

Dear all,

I was wondering which is the best way to set the location of a colorbar (associated to a heatmap) with makie.
I have tried the bbox argument without success. There is also the translate! function but it does not seem to apply to a colorbar.
I am basically trying to make a colorbar as inset (like what’s done with line plots there ).
I append (always the same :D) MWE below. I’d like to place the colorbar in the purple space located for x>1.5.

Cheers!

# Mesh
    x = LinRange(-1.0, 3, 200)
    y = LinRange(-2.0, 2.0, 100)
    Lx, Ly = x[end]-x[1], y[end]-y[1]
    # One continuous field 
    f = 1e3.*exp.(-x.^2  .- (y').^2)
    # One discontinuous field 
    g = zero(f)
    g[(x.^2 .+ (y.^2)') .< 0.5^2] .= 1.0
    # Compose figure
    fig = Figure(resolution = (600, 600), fontsize = 22, fonts = (;regular="CMU Serif"))
    ax = fig[1, 1] = Axis(fig, xlabel = L"x", ylabel = L"y", aspect=Lx/Ly)
    hm = GLMakie.heatmap!(ax, x, y, f, colormap = (:plasma))
    GLMakie.contour!(ax, x, y, g)
    GLMakie.Colorbar(fig[1, 2], hm, label = L"\log_{10}[(u^2+v^2)^{1/2}]", width = 20,
        labelsize = 14, ticklabelsize = 14, bbox=BBox(500, 550, 100, 500))
    DataInspector(fig)
    display(fig)
1 Like

If you specify a bbox you shouldn’t do Colorbar(fig[1, 2]) because that also sets the bbox using the layout mechanism. It’s either or.

x = LinRange(-1.0, 3, 200)
y = LinRange(-2.0, 2.0, 100)
Lx, Ly = x[end]-x[1], y[end]-y[1]
# One continuous field 
f = 1e3.*exp.(-x.^2  .- (y').^2)
# One discontinuous field 
g = zero(f)
g[(x.^2 .+ (y.^2)') .< 0.5^2] .= 1.0
# Compose figure
fig = Figure(resolution = (600, 600), fontsize = 22, fonts = (;regular="CMU Serif"))
ax = fig[1, 1] = Axis(fig, xlabel = L"x", ylabel = L"y", aspect=Lx/Ly)
hm = heatmap!(ax, x, y, f, colormap = (:plasma))
contour!(ax, x, y, g)

Colorbar(fig, hm, label = L"\log_{10}[(u^2+v^2)^{1/2}]", width = 20,
    labelsize = 14, ticklabelsize = 14, bbox=ax.scene.px_area,
    alignmode = Outside(10), halign = :right, ticklabelcolor = :white, labelcolor = :white,
    tickcolor = :white)

fig

2 Likes