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!
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} ).
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.
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' .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With