$ cd heatmap-1.0; python setup.py installRequires the Python Imaging Library.
import heatmap
import random
if __name__ == "__main__":
pts = []
for x in range(400):
pts.append((random.random(), random.random() ))
print "Processing %d points..." % len(pts)
hm = heatmap.Heatmap()
hm.heatmap(pts, "classic.png")
heatmap() takes a list of x, y coordinate tuples and produces a heatmap describing their density. Some highlights:
After creating the output image, you can call saveKML() to create a KML file for Google Earth. It assumes the list of input points are a series of lat/long coordinates in decimal degrees. When loaded into Google Earth, you will see the heatmap as a Ground Overlay.
import heatmap
import random
hm = heatmap.Heatmap()
pts = [(random.uniform(-77.012, -77.050), random.uniform(38.888, 38.910)) for x in range(100)]
hm.heatmap(pts, "classic.png")
hm.saveKML("data.kml")
A heatmap over downtown Washington, DC:

heatmap() comes with 5 color schemes:
![]() classic |
![]() fire |
![]() omg |
![]() pbj |
![]() pgaitch |
|
|
Available color schemes Colors at the top are used for the densest areas, the bottom the sparsest. |
|||||
hm = heatmap.Heatmap()
hm.heatmap(pts, "fire.png", scheme='fire')
heatmap() has only two required parameters:
| heatmap(self, points, fout, dotsize=150, opacity=128, size=(1024, 1024), scheme='classic')
| points -> an iteratable list of tuples, where the contents are the
| x,y coordinates to plot. e.g., [(1, 1), (2, 2), (3, 3)]
| fout -> output file for the PNG
| dotsize -> the size of a single coordinate in the output image in
| pixels, default is 150px. Tweak this parameter to adjust
| the resulting heatmap.
| opacity -> the strength of a single coordiniate in the output image.
| Tweak this parameter to adjust the resulting heatmap.
| size -> tuple with the width, height in pixels of the output PNG
| scheme -> Name of color scheme to use to color the output image.
| Use schemes() to get list. (images are in source distro)
Most options are self-explanatory, but dotsize and opacity could use some background.


This worked well enough for my datasets, but I had to tweak the algorithm several times. If it doesn't work for your data, send me feedback (and your source data, if you can.) I'll try to get more generalized algorithms in future releases.
The technique and color schemes came from gheat. If you need a web-enabled interface to heatmaps with Google Maps, gheat is a good tool for the job. I didn't do anything original, but merely took their work and generalized it. Chad credits the definitive heatmap as his starting point. Like those projects, this project is under the MIT license.