Ich habe sein Beispiel von einer anderen Seite versuchen "Make colorbar legend in Matplotlib/Cartopy", aber wenn ich versuche, es auszuführen in Jupyter Notebook wirft es Fehler wie folgt:Fehler erhalten KeyError: 'name_long' für 'land' 'records' object "name = country.attributes ['name_long']"
KeyError Traceback (most recent call last)
<ipython-input-3-55c282431f2e> in <module>()
14 ax = plt.axes(projection=ccrs.Robinson())
15 for country in shpreader.Reader(countries_shp).records():
---> 16 name = country.attributes['name_long']
17 num_users = countries[name]
18 ax.add_geometries(country.geometry, ccrs.PlateCarree(),
KeyError: 'name_long'
Bitte helfen! Codebeispiel:
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)
Wenn ich Tasten einzuloggen, es einen Schlüssel 'name_long' hat: print (sortiert (country.attributes.keys())) ['ABBREV', ... 'NAME', 'NAME_ALT', 'NAME_CIAWF', 'NAME_LEN', 'NAME_LONG', 'NAME_SORT', 'NOTE_ADM0', 'NOTE_BRK', 'POP_EST', .. .'WOE_NOTE ',' featurecla ',' scarelank '] –
'NAME_LONG' ist nicht dasselbe wie' name_long' –
Irgendwann haben sich die Natural Earth Metadaten geändert und die Schlüsselnamen wurden geändert. – pelson