我用cartopy和matplotlib制作了一个地理热力图,显示了我的应用程序用户数量的分布情况,但是添加颜色条例的过程中遇到了问题:
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
cmap = mpl.cm.Blues
# Countries is a dictionary of {"country_name": number of users}, for example
countries = {"United States": 100, "Canada": 50, "China": 10}
max_users = float(max(countries.values()))
shapename = 'admin_0_countries'
countries_shp = shpreader.natural_earth(resolution='110m', category='cultural', name=shapename)
ax = plt.axes(projection=ccrs.Robinson())
for country in shpreader.Reader(countries_shp).records():
name = country.attributes['name_long']
num_users = countries[name]
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=cmap(num_users/max_users, 1))
plt.savefig('iOS_heatmap.png', transparent=True, dpi=900)
我想添加一个颜色条图例。对于简单的matplotlib绘图,有文档可以这样做,但我不确定如何通过cartopy来完成,因为轴是GeoAxesSubplot
。如果您能帮忙添加图例就太好了。
我也希望得到提示,哪个库最适合这些地理热力图。我接下来必须制作一个美国用户的热力图,cartopy似乎不是最佳选择。谢谢!