1

我正在关注这个例子,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:

显然,我的逻辑并不完全正确。

4

1 回答 1

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

那是你要找的吗?

于 2016-07-08T19:40:12.893 回答