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

非 Y 不嫁゛ 提交于 2019-12-05 04:22:48

问题


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.

回答1:


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:

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:

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




回答2:


I am not so sure on the heat map, but to plot with coloured circles of different sizes you can use:

   from matplotlib import pyplot    

   pyplot.scatter(longitudes,latitudes,counts,c=rgb)
   pyplot.show()

where rgb is a 2-d array of user defined rgb values, something like:

   maxcount = float(max(counts))
   rgb = [[ 1, 0.5, x/maxcount ] for x in counts]

or however you wish to define your colours.




回答3:


In a general answer for any graphics library, you would want to do something like this:

maxSize = 10 #The maximum radius of the circles you wish to draw.
maxCount = max(counts)

for lat, long, count in zip(latitudes, longitudes, counts):
    draw_circle(lat, long, count/maxCount*maxSize) #Some drawing library, taking x, y, radius.

zip() allows you to join your three lists and iterate over them in one loop.

Dividing the count by the maximum count gives you a relative scale in size, which you then multiply up by the size you want the circles to be. If you wanted to change the colour too, you could do something like:

maxSize = 10 #The maximum radius of the circles you wish to draw.
maxCount = max(counts)

for lat, long, count in zip(latitudes, longitudes, counts):
    intensity = count/maxCount
    draw_circle(lat, long, intensity*maxSize, Color(intensity*255, 0, 0)) #Some drawing library, taking x, y, radius, colour.

Producing a sliding scale from black to red as intensity increases.

You may need to adjust the latitude and longitude values to produce sane x and y values, depending on the size you want in your final image and the values you are going to put in. If you find your counts get too large to display, and the smaller items too small when lowering the max size, you might want to consider a logarithmic scale instead of linear for the intensity.

Implementing this with an actual graphics library should be trivial, but depends on the library itself.



来源:https://stackoverflow.com/questions/9567528/python-how-to-plot-a-heatmap-for-coordinates-with-different-color-intensity-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!