我使用 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 似乎不是最好的选择。谢谢!