我正在制作一张地图,突出显示我所属组织中不同成员类型的国家/地区。我正在与cartopy一起制作这张地图,并以三种不同形状的蓝色突出显示三种类型的成员。 但是,每次我在Python启动器中运行此脚本时,都会不断收到此错误:
`Samuels-MacBook-Pro:IAPS samuelmathiasborer$ python3 map2018.py
Traceback (most recent call last):
File "map2018.py", line 303, in <module>
if country.attributes['adm0_a3'] in NCs:
KeyError: 'adm0_a3'`
我不确定为什么会显示此错误,因为我相信自己使用正确。任何帮助将不胜感激!
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs
#fig = plt.figure()
ax = plt.axes(projection=ccrs.Mercator(min_latitude=-57,max_latitude=77,central_longitude=10))
#ax.add_feature(cartopy.feature.LAND)
#ax.add_feature(cartopy.feature.OCEAN)
#ax.add_feature(cartopy.feature.COASTLINE)
#ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.5)
#ax.add_feature(cartopy.feature.LAKES, alpha=0.95)
#ax.add_feature(cartopy.feature.RIVERS)
ax.set_extent([-150, 60, -25, 60])
shpfilename = shpreader.natural_earth(resolution='110m',
category='cultural',
name='admin_0_countries')
reader = shpreader.Reader(shpfilename)
countries = reader.records()
# 3 letter country codes: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
# ABW Aruba
# AFG Afghanistan
# AGO Angola
# AIA Anguilla
# ALA Åland Islands
# ALB Albania
# AND Andorra
# ARE United Arab Emirates
# ARG Argentina
# ARM Armenia
# ASM American Samoa
# ATA Antarctica
# ATF French Southern Territories
# ATG Antigua and Barbuda
# AUS Australia
# AUT Austria
# AZE Azerbaijan
# BDI Burundi
# BEL Belgium
# BEN Benin
# BES Bonaire, Sint Eustatius and Saba
# BFA Burkina Faso
# BGD Bangladesh
# BGR Bulgaria
# BHR Bahrain
# BHS Bahamas
# BIH Bosnia and Herzegovina
# BLM Saint Barthélemy
# BLR Belarus
# BLZ Belize
# BMU Bermuda
# BOL Bolivia, Plurinational State of
# BRA Brazil
# BRB Barbados
# BRN Brunei Darussalam
# BTN Bhutan
# BVT Bouvet Island
# BWA Botswana
# CAF Central African Republic
# CAN Canada
# CCK Cocos (Keeling) Islands
# CHE Switzerland
# CHL Chile
# CHN China
# CIV Côte d'Ivoire
# CMR Cameroon
# COD Congo, the Democratic Republic of the
# COG Congo
# COK Cook Islands
# COL Colombia
# COM Comoros
# CPV Cabo Verde
# CRI Costa Rica
# CUB Cuba
# CUW Curaçao
# CXR Christmas Island
# CYM Cayman Islands
# CYP Cyprus
# CZE Czechia
# DEU Germany
# DJI Djibouti
# DMA Dominica
# DNK Denmark
# DOM Dominican Republic
# DZA Algeria
# ECU Ecuador
# EGY Egypt
# ERI Eritrea
# ESH Western Sahara
# ESP Spain
# EST Estonia
# ETH Ethiopia
# FIN Finland
# FJI Fiji
# FLK Falkland Islands (Malvinas)
# FRA France
# FRO Faroe Islands
# FSM Micronesia, Federated States of
# GAB Gabon
# GBR United Kingdom
# GEO Georgia
# GGY Guernsey
# GHA Ghana
# GIB Gibraltar
# GIN Guinea
# GLP Guadeloupe
# GMB Gambia
# GNB Guinea-Bissau
# GNQ Equatorial Guinea
# GRC Greece
# GRD Grenada
# GRL Greenland
# GTM Guatemala
# GUF French Guiana
# GUM Guam
# GUY Guyana
# HKG Hong Kong
# HMD Heard Island and McDonald Islands
# HND Honduras
# HRV Croatia
# HTI Haiti
# HUN Hungary
# IDN Indonesia
# IMN Isle of Man
# IND India
# IOT British Indian Ocean Territory
# IRL Ireland
# IRN Iran, Islamic Republic of
# IRQ Iraq
# ISL Iceland
# ISR Israel
# ITA Italy
# JAM Jamaica
# JEY Jersey
# JOR Jordan
# JPN Japan
# KAZ Kazakhstan
# KEN Kenya
# KGZ Kyrgyzstan
# KHM Cambodia
# KIR Kiribati
# KNA Saint Kitts and Nevis
# KOR Korea, Republic of
# KWT Kuwait
# LAO Lao People's Democratic Republic
# LBN Lebanon
# LBR Liberia
# LBY Libya
# LCA Saint Lucia
# LIE Liechtenstein
# LKA Sri Lanka
# LSO Lesotho
# LTU Lithuania
# LUX Luxembourg
# LVA Latvia
# MAC Macao
# MAF Saint Martin (French part)
# MAR Morocco
# MCO Monaco
# MDA Moldova, Republic of
# MDG Madagascar
# MDV Maldives
# MEX Mexico
# MHL Marshall Islands
# MKD Macedonia, the former Yugoslav Republic of
# MLI Mali
# MLT Malta
# MMR Myanmar
# MNE Montenegro
# MNG Mongolia
# MNP Northern Mariana Islands
# MOZ Mozambique
# MRT Mauritania
# MSR Montserrat
# MTQ Martinique
# MUS Mauritius
# MWI Malawi
# MYS Malaysia
# MYT Mayotte
# NAM Namibia
# NCL New Caledonia
# NER Niger
# NFK Norfolk Island
# NGA Nigeria
# NIC Nicaragua
# NIU Niue
# NLD Netherlands
# NOR Norway
# NPL Nepal
# NRU Nauru
# NZL New Zealand
# OMN Oman
# PAK Pakistan
# PAN Panama
# PCN Pitcairn
# PER Peru
# PHL Philippines
# PLW Palau
# PNG Papua New Guinea
# POL Poland
# PRI Puerto Rico
# PRK Korea, Democratic People's Republic of
# PRT Portugal
# PRY Paraguay
# PSE Palestine, State of
# PYF French Polynesia
# QAT Qatar
# REU Réunion
# ROU Romania
# RUS Russian Federation
# RWA Rwanda
# SAU Saudi Arabia
# SDN Sudan
# SEN Senegal
# SGP Singapore
# SGS South Georgia and the South Sandwich Islands
# SHN Saint Helena, Ascension and Tristan da Cunha
# SJM Svalbard and Jan Mayen
# SLB Solomon Islands
# SLE Sierra Leone
# SLV El Salvador
# SMR San Marino
# SOM Somalia
# SPM Saint Pierre and Miquelon
# SRB Serbia
# SSD South Sudan
# STP Sao Tome and Principe
# SUR Suriname
# SVK Slovakia
# SVN Slovenia
# SWE Sweden
# SWZ Swaziland
# SXM Sint Maarten (Dutch part)
# SYC Seychelles
# SYR Syrian Arab Republic
# TCA Turks and Caicos Islands
# TCD Chad
# TGO Togo
# THA Thailand
# TJK Tajikistan
# TKL Tokelau
# TKM Turkmenistan
# TLS Timor-Leste
# TON Tonga
# TTO Trinidad and Tobago
# TUN Tunisia
# TUR Turkey
# TUV Tuvalu
# TWN Taiwan, Province of China
# TZA Tanzania, United Republic of
# UGA Uganda
# UKR Ukraine
# UMI United States Minor Outlying Islands
# URY Uruguay
# USA United States of America
# UZB Uzbekistan
# VAT Holy See
# VCT Saint Vincent and the Grenadines
# VEN Venezuela, Bolivarian Republic of
# VGB Virgin Islands, British
# VIR Virgin Islands, U.S.
# VNM Viet Nam
# VUT Vanuatu
# WLF Wallis and Futuna
# WSM Samoa
# YEM Yemen
# ZAF South Africa
# ZMB Zambia
# ZWE Zimbabwe
# List each country only once, by the "most significant" membership
NCs = ['AUT','CAN','HRV','FIN','DEU','GRC','HUN','ITA','MKD','MEX','NLD','NOR','PRT','ESP','GBR','IRL','USA']
LCs = ['BEL','CMR','CRI','CZE','DNK','POL','IND','IDN','IRN','TUR','PHL','MLT','NPL','SRB','PAK','ROU','RUS','SVK','SWE','CHE','UKR','GHA','GRL','FRO']
IMs = ['ALB','BEN','BIH','EGY','FRA','GEO','GHA','IDN','LTU','MYS','MAR','NGA','SGP','LKA','VNM','CHN','CYP']
provs = []
# Set the border line width and colors
lwidth = 0.1
NCcolor = [2.0/255, 87.0/255, 250.0/255] # ~IAPS Blue
LCcolor = [30.0/255, 144.0/255, 255.0/255] # Dodger Blue
IMcolor = [0.0/255, 191.0/255, 255.0/255] # Deep Sky Blue
Provcolor = [135.0/255, 206.0/255, 250.0/255] # Light Sky Blue
Othercolor = [196.0/255, 199.0/255, 239.0/255] # Grey
# NCcolor = [2.0/255,87.0/255,250.0/255] # ~IAPS Blue
# LCcolor = [100.0/255, 230.0/255, 100.0/255] # Light Green
# IMcolor = [250.0/255,2.0/255, 153.0/255] # Purple
# Provcolor = [1, 0.5, 0] # Orange
# Othercolor = [196.0/255, 199.0/255, 239.0/255] # Grey
for country in countries:
if country.attributes['adm0_a3'] in NCs:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=(NCcolor),
label=country.attributes['adm0_a3'],linewidth=lwidth)
elif country.attributes['adm0_a3'] in LCs:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=(LCcolor),
label=country.attributes['adm0_a3'],linewidth=lwidth)
elif country.attributes['adm0_a3'] in IMs:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=(IMcolor),
label=country.attributes['adm0_a3'],linewidth=lwidth)
elif country.attributes['adm0_a3'] in provs:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=(Provcolor),
label=country.attributes['adm0_a3'],linewidth=lwidth)
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=(Othercolor),
label=country.attributes['adm0_a3'],linewidth=lwidth)
# Add labels
ax.text(-1.9e7,0,'National Committees',
bbox={'boxstyle':'round','facecolor':NCcolor, 'pad':0.6, 'edgecolor':'none'},fontsize=6)
ax.text(-1.9e7,-2.0e6,'Local Committees',
bbox={'boxstyle':'round','facecolor':LCcolor, 'pad':0.6, 'edgecolor':'none'},fontsize=6)
ax.text(-1.9e7,-4.0e6,'Individual Members',
bbox={'boxstyle':'round','facecolor':IMcolor, 'pad':0.6, 'edgecolor':'none'},fontsize=6)
if provs: ax.text(-1.9e7,-6.0e6,'Provisional Committees',
bbox={'boxstyle':'round','facecolor':Provcolor, 'pad':0.6, 'edgecolor':'none'},fontsize=6)
# Save in the same folder as map.png
plt.savefig('map2018.png', format='png', dpi=1000, bbox_inches='tight', pad_inches=0)
plt.savefig('map2018.ps', format='ps', dpi=1000, bbox_inches='tight', pad_inches=0)
plt.savefig('map2018.eps', format='eps', dpi=1000, bbox_inches='tight', pad_inches=0)
答案 0 :(得分:0)
您应尝试使用“ ADM0_A3”而不是“ adm0_a3”。在我看来,他们在今年的某个时候进行了更改。