Makie: heatmap with inset

Hi everyone,
I am trying to plot an inset inside of a heatmap and would like a semi-transparent background for the inset to make the line more visible.
However, it seems that all “background” attributes (i.e. axis ticks and background coloring) of the inset are overruled by the heatmap plot.
Here is a MWE that shows this problem, as you can see in the plot below, the inset actually works but its attributes need to be moved to the top layer.
How can this be done?

using CairoMakie
x = y = -5:1:5
z = [x^2 + y^2 for x in x, y in y]
fig = Figure(resolution = (600, 400))
ax = Axis(fig[1, 1]; aspect = 1)
hmap = heatmap!(x, y, z,)

inset_ax = Axis(fig[1,1],
    width=Relative(0.8),
    height=Relative(0.2),
    halign=0.5,
    valign=0.1,backgroundcolor = (:red,0.4)
    )

lines!(inset_ax,1:10
)
display(fig)

image

Here is a description how to achieve that currently Layouts - Julia Data Science

There is no global z-shift attribute for Axis that would make it easier, so a little bit of manual work is currently required to layer everything correctly.

1 Like

thanks, this solves the problem partially, althought the tick labels are still hidden behind the heatmap plot:
Any idea what can be done about this?
image

Ah I see the axis decorations were not translated, try like this:

function inset_axis!(fig, ax; z = 300, extent = (0.25, 0.75, 0.25, 0.75), axis_kwargs...)

    bbox = lift(ax.scene.px_area) do pxa
        _l, _b = minimum(pxa)
        _r, _t = maximum(pxa)
        l = _l + extent[1] * (_r - _l)
        r = _l + extent[2] * (_r - _l)
        b = _b + extent[3] * (_t - _b)
        t = _b + extent[4] * (_t - _b)
        BBox(l, r, b, t)
    end

    inset_ax = Axis(fig, bbox=bbox; axis_kwargs...)

    translate_forward!(x::Makie.Transformable, z) = translate!(Accum, x, 0, 0, z)
    translate_forward!(ax::MakieLayout.LineAxis, z) = foreach(x -> translate_forward!(x, z), values(ax.elements))
    translate_forward!(_) = nothing

    foreach(x -> translate_forward!(x, z), values(inset_ax.elements))
    translate!(Accum, inset_ax.scene, 0, 0, z)
    
    return inset_ax
end
f, ax, hm = heatmap(randn(10, 10), colormap = :Blues)
ax2 = inset_axis!(f, ax)
lines!(ax2, cumsum(randn(100)))
ax3 = inset_axis!(f, ax2, extent = (0, 0.5, 0, 1), z = 500, alignmode = Outside(10))
lines!(ax3, cumsum(randn(100)), color = Cycled(2))
f

2 Likes

Thank you for sharing this helper function, it works beautifully now!