Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to plot a heatmap for coordinates with different color intensity or different radius of circles?

Given some data in three lists, for example:

latitudes = [50.877979278564,48.550216674805,47.606079101562,50.772491455078,42.451354980469,43.074657440186,44.044174194336,44.563243865967,52.523406982422,50.772491455078]
longitudes = [4.700091838837, 9.038957595825, -122.333000183105, 7.190686225891, -76.476554870605, -89.403335571289, -123.070274353027, -123.281730651855, 13.411399841309, 7.190686225891]
counts = [15, 845, 2, 50, 95, 49, 67, 32, 1, 88]

which can be interpreted as: The coordinate of i which is (latitudes[i], longitudes[i]) occures counts[i] times on the map.

I want to generate a heatmap with an appropriate scale. The cordinates should be represented by colour filled circles. The diameter of the circles should somehow represent the count of the corresponding coordinate.

(As an alternative I thought about representing the count by colour intensity. I don't know which is best or if these two represantations can be combined.)

How can do I realize such a heatmap? (I assume it is called so?)

Perhaps it is relevant to mention the amount of data I am dealing with:

  • sum(counts) is about 1.000.000
  • there are around 25.000 different coordinates.
like image 872
Aufwind Avatar asked Oct 12 '25 08:10

Aufwind


1 Answers

scatter is the method you are looking for, at it has two optional parameters to either adjust the size (with keyword size or just s) or the color (with keyword color or c) of each point, or you can do both simultaneously. The color, or heatmap effect, is probably better for the density of points you have.

Here's an example of using this method:

import matplotlib.pyplot as plt
import numpy as np

NPOINTS = 1000

np.random.seed(101)
lat = np.random.random(NPOINTS)*8+44
lon = np.random.random(NPOINTS)*100-50
counts = np.random.randint(0,1000,NPOINTS)

plt.subplot(211)
plt.scatter(lat, lon, c=counts)
plt.colorbar()
plt.subplot(212)
plt.scatter(lat, lon, s=counts)

plt.savefig('scatter_example.png')
plt.show()

Resulting in:

enter image description here

If you choose to use size, you might want to adjust the count values to get a less crowded plot, for example by extending the above example with:

plt.figure()
COUNT_TO_SIZE = 1./10
plt.scatter(lat, lon, s=counts*COUNT_TO_SIZE)
plt.savefig('scatter_example2.png')

You get a cleaner plot:

enter image description here

I've of course accidentally swapped latitude and longitude from their normal axes, but you get the idea :)

like image 197
Yann Avatar answered Oct 14 '25 19:10

Yann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!