Fill countries in python basemap

后端 未结 3 525
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 02:23

Hi I am trying to plot a map using pythons basemap with some countries filled in a certain colour.

Is there a quick and easy solution out there??

3条回答
  •  伪装坚强ぢ
    2020-11-30 02:27

    Updating @pelson answer for Python 3:

    import cartopy.crs as ccrs
    import matplotlib.pyplot as plt
    import cartopy.io.shapereader as shpreader
    import itertools
    import numpy as np
    
    shapename = 'admin_0_countries'
    countries_shp = shpreader.natural_earth(resolution='110m',
                                            category='cultural', name=shapename)
    
    print(countries_shp)
    
    # some nice "earthy" colors
    earth_colors = np.array([(199, 233, 192),
                             (161, 217, 155),
                             (116, 196, 118),
                             (65, 171, 93),
                             (35, 139, 69),
                            ]) / 255
    earth_colors = itertools.cycle(earth_colors)
    
    ax = plt.axes(projection=ccrs.PlateCarree())
    
    for country in shpreader.Reader(countries_shp).records():
        print(country.attributes['NAME_LONG'], next(earth_colors))
        ax.add_geometries(country.geometry, ccrs.PlateCarree(),
                          facecolor=next(earth_colors),
                          label=country.attributes['NAME_LONG'])
    
    plt.show()
    

提交回复
热议问题