I'm coloring countries on a cartopy map according to certain values. I'm using geopandas and a shapefile from: https://www.naturalearthdata.com/
While iterating over the dataframe df to get the geometry of certain countries I encountered a problem. I can get the geometry of Countries with a Multipolygon geometry, but I can't get do it with countries with a polygon geometry eg Belgium or Austria.
Here is my code:
#imports
import matplotlib.pyplot as plt
import matplotlib
import cartopy
from cartopy.io import shapereader
import cartopy.crs as ccrs
import geopandas
import numpy as np
# get natural earth data (http://www.naturalearthdata.com/)
# get country borders
resolution = '10m'
category = 'cultural'
name = 'admin_0_countries'
shpfilename = shapereader.natural_earth(resolution, category, name)
# read the shapefile using geopandas
df = geopandas.read_file(shpfilename)
# Set up the canvas
fig = plt.figure(figsize=(20, 20))
central_lon, central_lat = 0, 45
extent = [-10, 28, 35, 65]
ax = plt.axes(projection=cartopy.crs.Orthographic(central_lon, central_lat))
ax.set_extent(extent)
ax.gridlines()
# Add natural earth features and borders
ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=0.8)
ax.add_feature(cartopy.feature.OCEAN, facecolor=("lightblue"))
ax.add_feature(cartopy.feature.LAND, facecolor=("lightgreen"), alpha=0.35)
ax.coastlines(resolution='10m')
# Countries and value
countries = ['Sweden', 'Netherlands', 'Ireland', 'Denmark', 'Germany', 'Greece', 'France', 'Spain', 'Portugal', 'Italy', 'United Kingdom', 'Austria']
value = [47.44, 32.75, 27.53, 23.21, 20.08, 18.08, 17.23, 13.59, 12.13, 5.66, 22.43, 7]
# Normalise values
value_norm = (value-np.nanmin(value))/(np.nanmax(value) - np.nanmin(value))
# Colourmap
cmap = matplotlib.cm.get_cmap('YlOrBr')
for country, value_norm in zip(countries, value_norm):
# read the borders of the country in this loop
poly = df.loc[df['ADMIN'] == country]['geometry'].values[0]
# get the color for this country
rgba = cmap(value_norm)
# plot the country on a map
ax.add_geometries(poly, crs=ccrs.PlateCarree(), facecolor=rgba, edgecolor='none', zorder=1)
# Add a scatter plot of the original data so the colorbar has the correct numbers
dummy_scat = ax.scatter(value, value, c=value, cmap=cmap, zorder=0)
fig.colorbar(mappable=dummy_scat, label='Percentage of Do and Dont`s [%]', orientation='horizontal', shrink=0.8)
plt.show()
fig.savefig("Länderübersicht.jpg")
How can I iterate over, or rather color, these countries or do I have to get another shapefile? Thanks!
The command ax.add_geometries()
asks for a list of geometries, so that, a single geometry will cause an error. To fix your code, you can replace the line:
ax.add_geometries(poly, crs=ccrs.PlateCarree(), facecolor=rgba, edgecolor='none', zorder=1)
with these lines of code:
# plot the country on a map
if poly.geom_type=='MultiPolygon':
# `poly` is a list of geometries
ax.add_geometries(poly, crs=ccrs.PlateCarree(), facecolor=rgba, edgecolor='none', zorder=1)
elif poly.geom_type=='Polygon':
# `poly` is a geometry
# Austria, Belgium
# Plot it `green` for checking purposes
ax.add_geometries([poly], crs=ccrs.PlateCarree(), facecolor="green", edgecolor='none', zorder=1)
else:
pass #do not plot the geometry
Note that if poly.geom_type
is 'Polygon', I just use [poly] in place of poly
.
Taking inspiration from the error code TypeError: 'Polygon' object is not iterable
I started from the assumption that we need some kind of iterable, such as a list of polygons. Drawing from this answer I found the function shapely.geometry.MultiPolygon
does the job. You simply pass it a list of polygons. Add a little logic to take this action only when a Polygon
rather than a MultiPolygon
is detected and we have:
poly = df.loc[df['ADMIN'] == country]['geometry'].values[0]
if type(poly) == shapely.geometry.polygon.Polygon:
simple_poly = df.loc[df['ADMIN'] == country]['geometry'].values[0]
list_polys = [poly, poly]
poly = shapely.geometry.MultiPolygon(list_polygons)
This is a rather hacky solution that will print the polygon twice, so be aware if you later decide to make it transparent or something. Alterantively, in place of [poly, poly]
you could use [poly, some_other_poly_outside_map_area]
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.