Is there a way to have heatmap and contours from other variables in Plots.jl with GR?

Hi all!

I’m trying to plot a heat map of one variable (with all values >>0), but on the same plot, I want to plot contours of two other variables where they equal 0.
When I try countour! after plotting the heatmap, the colors of the heatmap go off, I guess because the other variables are 0.

my attempt is something like this:

dQTdP = getindex.(∇Q_T, 1)   # ∂QT/∂P, same size as ∇Q
dQTdδ = getindex.(∇Q_T, 2)   # ∂QT/∂δ

# Gradient magnitude
mag = hypot.(dQTdP, dQTdδ)

cl = extrema(mag)   # freeze based on heatmap data

heatmap(P_range, δ_range, mag;
    xlabel="P", ylabel="δ",
    title="‖∇Q_T‖",
    color=:viridis,
    clims=cl,
    colorbar_title="magnitude",
    legend=true)

# Now all overlays must respect the same clims
contour!(P_range, δ_range, detJ;
    levels=[-0.15, 0.15],
    linecolor=:black,
    linestyle=:dash,
    linewidth=1.5,
    colorbar=false,
    line_z=nothing)

contour!(P_range, δ_range, Q_total;
    levels=[0.0],
    linecolor=:green,
    linewidth=2,
    colorbar=false,  
    line_z=nothing)

Is there a way to pull this off?

You could shift the additional datasets and their contour levels to the mean z0 of the primary dataset and request that the z0 contour be plotted for each shifted version:

z0 = mean(mag)

p = heatmap(P_range, δ_range, mag; xlabel="P", ylabel="δ", title="‖∇Q_T‖", color=:viridis, clims=cl)

levels1 = [-0.15, 0.15]
for lvli in levels1
    contour!(P_range, δ_range, z0 .+ detJ .- lvli; levels=[z0], clims=cl, lc=:black, ls=:dash, lw=1.5)
end

levels2 = [0.0]
for lvli in levels2
    contour!(P_range, δ_range, z0 .+ Q_total .- lvli; clims=cl, levels=[z0], lc=:green, lw=2)
end

p

Perhaps a default white background of the contour plot covers everything below:

background_color = :transparent

See also