top | item 19689396

Creating Heatmap from Scratch in Python (2018)

70 points| geomatics99 | 6 years ago |geodose.com

23 comments

order
[+] alexhutcheson|6 years ago|reply
I like seaborn's jointplot()[1] function for generating plots like this. I actually prefer the "hexagonal bin" plot, but jointplot() can also do kernel density estimation like this post does.

Hexbin example: https://seaborn.pydata.org/examples/hexbin_marginals.html

KDE example: https://seaborn.pydata.org/examples/joint_kde.html

If you are doing any exploratory analysis using scatterplots, I highly recommend that you also generate hexbin plot or heatmap. Far too often it turns out all of the points are concentrated in a small area of the plot, but that's not always apparent on a scatterplot.

[1] https://seaborn.pydata.org/generated/seaborn.jointplot.html

[+] anc84|6 years ago|reply
Please be aware that you MUST NOT use this for geographic coordinates such as "GPS" or "WGS84" latitude & longitude pairs. Depending on the longitude areas are of different sizes. You must use code that knows about that or your results will be utterly wrong.
[+] mturmon|6 years ago|reply
The OP makes a related mistake (although for their data, it worked out OK). It uses this grid:

  x_grid=np.arange(x_min-h,x_max+h,grid_size)
  y_grid=np.arange(y_min-h,y_max+h,grid_size)
where h is a small constant, and the range ends are the min/max of the data. In general, this will result in rectangular pixels, which you probably don't want.

In your example, even for a small geographic area, a degree of longitude will typically translate to a smaller distance than a degree of latitude, so as you say you can't treat them the same.

[+] peteradio|6 years ago|reply
This would only skew density maps not counts maps.
[+] kuzehanka|6 years ago|reply
How does this factor for tools like Datashader?
[+] yetihehe|6 years ago|reply
I much prefer kriging, which seems to better model sparse areas. https://en.wikipedia.org/wiki/Kriging too bad there are no image comparisons for those methods. Main difference - instead of sum of distances with cutoff, you make sum of values weighted by distances for each map point.
[+] plopz|6 years ago|reply
What you describe sounds more like idw than kriging.
[+] 4thaccount|6 years ago|reply
Neat...I just wish there was an easy way to give the basemap library a 3-item tuple with (lat, lon, value) and have it automatically do a heat map and not just plot the points. That is an option in some software.
[+] rakshitadmar|6 years ago|reply
I have something of the sort. Give it later, lon and a value, it performs cubic interpolation and forms a heatmap. Some of the code is specific to my application but I'm sure you can reuse.

https://github.com/rraks/sigcatch

Open to contributions.

[+] alexhutcheson|6 years ago|reply
If you're making heatmaps with GIS data, give some critical thought to the interpretation of the map to make sure you're not just doing this: https://xkcd.com/1138/