Heatmap of data not on a grid

I have been trying to make a heatmap of some experimental data using Makie. When the x,y positions of the datapoints are on a grid, this works just fine:

using CairoMakie

i = 1
for x in 1:5
    for y in 1:5
        xys[i,:] = [x, y]
        i = i+1
    end
end
xs = xys[:,1]
ys = xys[:,2]
zs = rand(25)
heatmap(xs,ys,zs)

But when the grid points are randomly places (ie. experimental data), the plot gets all wonky:

using CairoMakie

i = 1
for x in 1:5
    for y in 1:5
        xys[i,:] = [x+rand()/5-0.1, y+rand()/5-0.1]
        i = i+1
    end
end
xs = xys[:,1]
ys = xys[:,2]
zs = rand(25)
heatmap(xs,ys,zs)

My first thought was to solve this by interpolating the data, but this seems to also work only with data that is already on a grid…

Any ideas on how to solve this?

See for instance this example using the KernelInterpolation.jl package.

1 Like

You can use PyPlot.jl (or PythonPlot.jl) or GMT.jl, for example, both of which support this by triangulating irregular data onto a mesh and then linearly interpolating from that.

(There are also various packages that can do unstructured-points interpolation, see e.g. 2D Interpolation on an irregular grid, also Dierckx.jl and BasicInterpolators.jl and KernelInterpolation mentioned above.)

Your code doesn’t run, but cant you just use tricontourf as an alternative?

2 Likes

When you create a heatmap using three vectors in makie, then it infers the structure of the matrix from that data. If your data is not actually on a rectangular grid, the output is necessarily wonky. Pass the x and y values yourself and your z data as a matrix if there are too many missing datapoints. Otherwise tricontourf might be a better fit, as mentioned above.

I ended up going with KernelInterpolation.jl as suggested by Rafael. using tricontourf also works, but the plot does not look as nice as a heatmap.
Thanks you all for the suggestions!

2 Likes

I’m happy to see you used KernelInterpolation.jl. Would you mind sharing the code you used for future reference?