2016-07-06 9 views
1

Ich folge diesem Beispiel, Python Mapping in Matplotlib Cartopy Color One Country. Es funktioniert voll mit mehreren Ländern, z. USA, Frankreich, Großbritannien, Japan.Cartopy-Python-Syntax - mehrere Objekte/Länder in einer Zeile

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

Ich würde gerne eine Liste von Ländern in einer Zeile setzen, anstatt die Aussagen immer und immer wieder zu wiederholen.

Ich habe versucht:

if country.attributes['adm0_a3'] == ['USA', 'FRA', 'GBR', 'JPN']: 

Und any('USA, 'FRA', 'GBR', 'JPN')

Und ['USA or 'FRA' or 'GBR' or'JPN']

Und ein dict:

myDict = {'USA', 'FRA', 'GBR', 'JPN'} 
if country.attributes['adm0_a3'] == myDict: 

Natürlich, ich bin nicht die Logik ganz richtig hinzubekommen.

Antwort

2

sollten Sie verwenden das in Schlüsselwort, so etwas wie dieses:

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

Ist das, was Sie suchen?

+0

Mehrere Stunden Schmerzen für zwei Buchstaben! :Oh, danke. –