我跟随此示例Python Mapping in Matplotlib Cartopy Color One Country。它与几个国家完全合作,例如美国,法国,英国,日本。
for country in countries:
if country.attributes['adm0_a3'] == 'USA':
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor='#008744', alpha = 0.5,
label=country.attributes['adm0_a3']),
if country.attributes['adm0_a3'] == 'FRA':
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor='#008744', alpha = 0.5,
label=country.attributes['adm0_a3']),
+ 'GBR'
+ 'JPN'
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=('#c4e6ff'),
label=country.attributes['adm0_a3'])
我希望将国家列表放在一行,而不是一遍又一遍地重复这些陈述。
我试过了:
if country.attributes['adm0_a3'] == ['USA', 'FRA', 'GBR', 'JPN']:
any('USA, 'FRA', 'GBR', 'JPN')
['USA or 'FRA' or 'GBR' or'JPN']
一个字典:
myDict = {'USA', 'FRA', 'GBR', 'JPN'}
if country.attributes['adm0_a3'] == myDict:
显然,我没有把逻辑做得恰到好处。
答案 0 :(得分:2)
您应该使用in
关键字,如下所示:
for country in countries:
if country.attributes['adm0_a3'] in ['USA', 'FRA', 'GBR', 'JPN']:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=(0, 0, 1),
label=country.attributes['adm0_a3'])
else:
ax.add_geometries(country.geometry, ccrs.PlateCarree(),
facecolor=('#c4e6ff'),
label=country.attributes['adm0_a3'])
那是你在找什么?