Makie - Inset axes and their drawing order

If I understand correctly inset axes can be create rather easily in Makie by putting multiple axes in the same layout position. Something as follow:

fig = Figure(resolution=(500, 500))

# The main axis
density_ax = Axis(fig[1, 1],
    backgroundcolor=:white)

# Some function plotting stuff
plot_densities!(density_ax, momenta[:, :, end, :])

# The inset axis
inset_ax = Axis(fig[1, 1],
    width=Relative(0.5),
    height=Relative(0.5),
    halign=0.9,
    valign=0.9,
    backgroundcolor=:lightgray)

hidedecorations!(inset_ax)

# Some function plotting stuff
plot_densities!(inset_ax, momenta[:, :, end, :])

However it appears that the inset axis is drawn behind some elements of the main axis. For example in the following the grid and the contourf are drawn over the inset axis.

image

I tried to change the order in which I define the axis but it didn’t change much. How could I control that such that the inset axis would cover everyting drawn on the main axis?

1 Like

The render order in this case is based on the z-value of each component. I’m not aware of a nice way to change this for the whole axis, but you can try this to move its components forward individually:

for (k, v) in inset_ax.elements
    if v isa AbstractPlot
        translate!(Accum, v, Vec3f0(0, 0, 200))
    else
        for (k, v2) in v.elements
            translate!(Accum, v2, Vec3f0(0, 0, 200))
        end
    end
end
translate!(Accum, inset_ax.scene, Vec3f0(0, 0, 200))

It seems like it comes from transparency of inset plot but I do not know enough about it to change alpha value of colors.

Maybe it’s easier to translate the scene of the outer axis backwards, and probably also the grid lines. But otherwise like @ffreyer said.

You need to handle the background rect:

fig = Figure(resolution=(500, 500))

# The main axis
density_ax = Axis(fig[1, 1],
    backgroundcolor=:white)

# Some function plotting stuff
density!(density_ax, rand(1000))

# The inset axis
inset_ax = Axis(fig[1, 1],
    width=Relative(0.5),
    height=Relative(0.5),
    halign=0.9,
    valign=0.9,
    backgroundcolor=:lightgray)
hidedecorations!(inset_ax)
translate!(inset_ax.scene, 0, 0, 10)
# this needs separate translation as well, since it's drawn in the parent scene
translate!(inset_ax.elements[:background], 0, 0, 9)
scatter!(inset_ax, 0..1, rand(4))
fig

So this works :wink:

Maybe we can give the axis constructor a Z-index keyword argument, to make this easier!

3 Likes

Translating background and scene work just fine, thanks a lot.

Maybe we can give the axis constructor a Z-index keyword argument, to make this easier!

I think that would be sufficient to make Makie one of the plotting library with the most streamlined inset axis workflow :slight_smile: