Plot contours of a heatmap

I have this heatmap of a matrix with Integer values.

But it is best suited for continuous values, so I tried contour instead but the contour links the center of the squares like this:

Is there an easy way to trace the contours that you see in the heatmap ?

This looks like Plots.jl, so I’ll assume you’re using that.
For me, this makes smooth contours:

using Plots
A = [(x-5)*(y-5) for x=1:10,y=1:11]
contour(A, w=2, linecolor=:black, fill=true)

What version of Plots are you using? Which backend (default GR)? Can you provide a MWE? This makes it easier to help you.

I’m using Plots 0.29.6 yes, with GR.

try this:

A = reshape([max(i,j) for i in 1:5 for j in 1:5],5,5)
heatmap(A)
contourf(A, levels = 1:5)

I would like contour to draw the same squares as the heatmap.

Sorry for misunderstanding. I don’t know of a straightforward solution, but maybe you can use a hack like this:

using Plots

A = reshape([max(i,j) for i in 1:5 for j in 1:5],5,5)

function scale4plot(A,f=100)
    resize(A,f) = [A[i,j] for i=1 .+(0:f*size(A,1)-1).÷f,j=1 .+(0:f*size(A,2)-1).÷f]
    B = resize(A,f)
    ax,ay = ( (axes(B,i)./f).+0.5 for i=1:2)
    ay,ax,B
end
contour(scale4plot(A)..., w=1, linecolor=:black, fill=true )

image

1 Like