Overlay a contour over a heatmap

Although, I’m afraid one of them is wrong. The contour lines are different. For me it makes sense to have lines around the big jumps, because the values are lower. I’m not sure what’s happening in the Plots solution. Increasing the grid points it’s more clear.

using CairoMakie
f(x,y) = (x + 2y^2) * abs(sin(y) + cos(x))
x = y = 1:0.2:20
z = [f(x,y) for x in x, y in y]
fig = Figure(resolution=(750,300), fontsize = 14)
ax1 = Axis(fig, aspect = 1, xlabel = "x", ylabel = "y")
ax2 = Axis(fig, aspect = 1,  xlabel = "x")
ax3 = Axis(fig, aspect = 1,  xlabel = "x")
p1 = heatmap!(ax1, x, y, z, colormap = :plasma)
contour!(ax2, x, y, z, color = :black, levels = 100:1:101) # contourf! also works... but with colormap
heatmap!(ax3, x, y, z, colormap = (:plasma, 0.5))
contour!(ax3, x, y, z, color = :white, levels = 100:1:101)
cbar = Colorbar(fig, p1, width = 10, hight = Relative(0.8), ticklabelsize = 10, tickalign = 1)
limits!(ax2, 1,20,1,20)
hideydecorations!(ax2, grid = false)
hideydecorations!(ax3, grid = false)
fig[1,1] = ax1
fig[1,2] = cbar
fig[1,3] = ax2
fig[1,4] = ax3
#save("./results/FigMixHeatContour.png", fig, px_per_unit = 2)
fig

2 Likes