2017-11-26 4 views
0

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) 
+0

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 '] –

+0

'NAME_LONG' ist nicht dasselbe wie' name_long' –

+0

Irgendwann haben sich die Natural Earth Metadaten geändert und die Schlüsselnamen wurden geändert. – pelson

Antwort

1

Ihr country.attributes Wörterbuch/Karte hat keinen Wert für den Schlüssel 'name_long'.

Ihr Land ist ein Record wie von der angegeben.

Datensätze haben Attribute, die nur ein einfaches Wörterbuch sind. Welche Daten Sie auch lesen, hat kein 'name_long' Attribut.

Sie scheinen downloading the data von der NaturalEarthData Website zu sein. Überprüfen Sie also, welche Attribute von dort tatsächlich verfügbar sind.

Wie pro Ihren Kommentar, erscheinen Sie die 'NAME_LONG' Schlüssel zu haben, aber ... 'NAME_LONG' != 'name_long'

Verwandte Themen