# Heatmap of data not on a grid

**URL:** <https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022>\
**Category:** General Usage\
**Tags:** plotting, interpolations, makie\
**Created:** [February 18, 2025, 10:06am UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022 "2025-02-18T10:06:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanderveer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanderveer/32/37519_2.png) [@evanderveer](https://discourse.julialang.org/u/evanderveer)\
**Post date:** [February 18, 2025, 10:06am UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022/1 "2025-02-18T10:06:40Z")

</div>

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:

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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/e/ae24ae4cb991c3b97c764a1b8b882e5885065c35.png)

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

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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/a/da80ab4bf352f4580eb3e05a8ea2aab532c86d55.png)

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?

---

<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:** [February 18, 2025, 10:39am UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022/2 "2025-02-18T10:39:11Z")

</div>

See for instance [this example](https://discourse.julialang.org/t/3d-interpolation-in-julia/117035/20) using the KernelInterpolation.jl package.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 18, 2025, 12:31pm UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022/3 "2025-02-18T12:31:01Z")

</div>

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.

> [@Heatmap plots with X and Y matrix instead of vectors](https://discourse.julialang.org/t/heatmap-plots-with-x-and-y-matrix-instead-of-vectors/89665):
>
> Hi all, I want to know if it is possible to make heatmap plots type with X and Y matrix instead of using X and Y vectors. In fact my X and Y matrix are longitude and latitude and the Z matrix are geostationnary satellites radiances where all matrix have same size ; 3712x3712 up to 12000x12000. for all matrix their corners are nan (space pixels). I didnot find examples on this website [Home · Plots](https://docs.juliaplots.org/latest/) later I will have to draw the cost lines over this image plot. I used to use Matlab ( pcolor(X,Y…

(There are also various packages that can do unstructured-points interpolation, see e.g. [2D Interpolation on an irregular grid](https://discourse.julialang.org/t/2d-interpolation-on-an-irregular-grid/105247), also Dierckx.jl and BasicInterpolators.jl and KernelInterpolation mentioned above.)

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [February 18, 2025, 1:04pm UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022/4 "2025-02-18T13:04:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [February 18, 2025, 4:27pm UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022/5 "2025-02-18T16:27:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![evanderveer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanderveer/32/37519_2.png) [@evanderveer](https://discourse.julialang.org/u/evanderveer)\
**Post date:** [February 19, 2025, 8:05am UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022/6 "2025-02-19T08:05:33Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![JoshuaLampert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joshualampert/32/205964_2.png) [@JoshuaLampert](https://discourse.julialang.org/u/JoshuaLampert)\
**Post date:** [February 19, 2025, 9:04am UTC](https://discourse.julialang.org/t/heatmap-of-data-not-on-a-grid/126022/7 "2025-02-19T09:04:38Z")

</div>

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