Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make colorbar legend in Matplotlib/Cartopy

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!

like image 712
goodcow Avatar asked Dec 22 '16 20:12

goodcow


People also ask

How do I add a legend to my Colorbar?

To plot data and draw a colorbar or legend in one go, pass a location (e.g., colorbar='r' or legend='b' ) to the plotting command (e.g., plot or contour ). To pass keyword arguments to the colorbar and legend commands, use the legend_kw and colorbar_kw arguments (e.g., legend_kw={'ncol': 3} ).

How do I change the color bar in Matplotlib?

You can change the color of bars in a barplot using color argument. RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.

How do I change the location of my color bar?

To move the colorbar to a different tile, set the Layout property of the colorbar. To display the colorbar in a location that does not appear in the table, use the Position property to specify a custom location. If you set the Position property, then MATLAB sets the Location property to 'manual' .


1 Answers

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.

like image 61
Deditos Avatar answered Oct 11 '22 14:10

Deditos