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

PeX
Tue, 26 Aug 2025 20:21:57 +0000
Visualization

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?

3 posts - 3 participants