Make colorbar legend in Matplotlib/Cartopy

好久不见. 提交于 2019-12-07 02:48:52

问题


I've made a geographical heatmap using cartopy and matplotlib, of the number of users of my app, but am having trouble adding a colorbar legend:

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)

which produces

I want to add a colorbar legend. There is documentation to do so for a straightforward matplotlib plot, but I'm not sure how to do it through cartopy, where the axis is a GeoAxesSubplot. Any help adding the legend would be appreciated.

I would also appreciate tips as to what library is best for these sorts of geographical heatmaps. I have to make a heatmap of users in the US next, and cartopy doesn't seem to be the best option. Thanks!


回答1:


I think that add_geometries() returns a FeatureArtist rather than some Matplotlib mappable object that can be passed to colorbar(). The simplest solution I can think of is to create your own mappable and use that to create the colorbar. Try placing these lines after your country loop:

sm = plt.cm.ScalarMappable(cmap=cmap,norm=plt.Normalize(0,1))
sm._A = []
plt.colorbar(sm,ax=ax)

Incidently, this kind of geographical heatmap is called a choropleth map.



来源:https://stackoverflow.com/questions/41291534/make-colorbar-legend-in-matplotlib-cartopy

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