I want to plot the map of Mexico and shade the states accordingly to a the values of a dictionary. I used the following code suggested in previous question (Easiest way to plot data on country map with python), so far it plots the country and states, but when I try to define the shading I get an error. Below the code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from mpl_toolkits.basemap import Basemap
%matplotlib inline
from shapely.geometry import Polygon
mexican_states_people = {'AGS': 20,'BC': 57, 'BCS': 562, 'CAMP': 594,'CHIH': 442,'CHIS': 69,'COAH': 100,'COL': 237,'DF': 7323,'DGO': 689,'GRO': 40,'GTO': 295,'HGO': 1134,'JAL': 875,'MEX': 1,'MICH': 393, 'MOR': 301,'NAY': 404,'NL': 327,'OAX': 391,'PUE': 670,'QRO': 270,'QROO': 156,'SIN': 63,'SLP': 689,'SON': 291,'TAB': 306,'TAMPS': 59,'TLAX': 108,'VER': 17,'YUC': 35,'ZAC': 890}
m = Basemap(llcrnrlon=-115,llcrnrlat=5,urcrnrlon=-80,urcrnrlat=35,
resolution='i',projection='tmerc',lon_0=-99,lat_0=19)
m.readshapefile("MEX_adm1", "mexican_states")
max_people = np.max(mexican_states_people.values())
for coordinates, state in zip(m.mexican_states, m.mexican_states_info):
print state
if state["State_name"] in mexican_states_people.keys():
shade = mexican_states_people[state["State_name"]]/max_people
m.drawcoastlines()
m.fillcontinents(color='coral',lake_color='aqua')
m.drawparallels(np.arange(-40,61.,2.))
m.drawmeridians(np.arange(-20.,21.,2.))
m.drawmapboundary(fill_color='aqua')
plt.title("Mexico")
plt.show()
I get error: KeyError: 'State_name'
.
The print state
command gave the following information:
{'NAME_0': 'Mexico', 'NAME_1': 'Aguascalientes', 'TYPE_1': 'Estado', 'CCA_1': ' ', 'VARNAME_1': ' ', 'ENGTYPE_1': 'State', 'HASC_1': 'MX.AG', 'RINGNUM': 1, 'ID_0': 145, 'ID_1': 1, 'ISO': 'MEX', 'NL_NAME_1': ' ', 'CCN_1': 0, 'SHAPENUM': 1}
The shapefile "MEX_adm1" was downloaded from http://www.gadm.org/