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

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-have-heatmap-and-contours-from-other-variables-in-plots-jl-with-gr/131869>\
**Category:** Visualization\
**Tags:** plots, gr\
**Created:** [August 26, 2025, 8:21pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-heatmap-and-contours-from-other-variables-in-plots-jl-with-gr/131869 "2025-08-26T20:21:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pex/32/49986_2.png) [@PeX](https://discourse.julialang.org/u/PeX)\
**Post date:** [August 26, 2025, 8:21pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-heatmap-and-contours-from-other-variables-in-plots-jl-with-gr/131869/1 "2025-08-26T20:21:57Z")

</div>

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:

```julia-auto
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?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 27, 2025, 7:10am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-heatmap-and-contours-from-other-variables-in-plots-jl-with-gr/131869/2 "2025-08-27T07:10:55Z")

</div>

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:

```julia-auto
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

```

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [August 27, 2025, 11:59am UTC](https://discourse.julialang.org/t/is-there-a-way-to-have-heatmap-and-contours-from-other-variables-in-plots-jl-with-gr/131869/3 "2025-08-27T11:59:18Z")

</div>

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

`background_color = :transparent`

[See also](https://discourse.julialang.org/t/save-figure-with-transparent-background-color-in-plots-jl/18808)
