Make colorbar legend in Matplotlib/Cartopy

6.5k views Asked by At

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 enter image description here

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

There are 1 answers

0
Deditos On

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)

choropleth with colorbar

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