Python:如何为具有不同颜色强度或不同圆半径的坐标绘制热图? [英] Python: How to plot a heatmap for coordinates with different color intensity or different radius of circles?

查看:350
本文介绍了Python:如何为具有不同颜色强度或不同圆半径的坐标绘制热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出三个列表中的一些数据,例如:

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]

可以解释为:i的坐标为(latitudes[i]longitudes[i])在地图上出现counts[i]次.

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.)

如何实现这样的热图? (我想是这样吗?)

也许提及我正在处理的数据量很重要:

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

  • sum(counts)约为1.000.000
  • 大约有25.000个不同的坐标.
  • sum(counts) is about 1.000.000
  • there are around 25.000 different coordinates.

推荐答案

scatter是您要寻找的方法,它具有两个可选参数来调整大小(使用关键字size或仅s )或每个点的颜色(使用关键字colorc),或者您可以同时执行两个操作.颜色或热图效果可能对您拥有的点的密度更好.

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()

结果:

如果选择使用size,则可能需要调整计数值,以减少图的拥挤程度,例如,将上述示例扩展为:

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 :)

这篇关于Python:如何为具有不同颜色强度或不同圆半径的坐标绘制热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆