【问题标题】:How to use cartopy to create colored US states如何使用 cartopy 创建彩色美国各州
【发布时间】:2018-11-13 22:42:08
【问题描述】:

我需要创建一个地图,其中状态具有不同的颜色,具体取决于有关该状态的一条数据。我在 cartopy 库中找到了一个 example of a US map,但它没有演示如何引用状态并访问它们的属性,而且几乎没有其他内容:

从示例中,我已将他们的代码简化为以下内容,如果能帮助我修改此代码以根据状态的人口密度大小设置状态的面部颜色,我将不胜感激。

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader

fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())

ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())

shapename = 'admin_1_states_provinces_lakes_shp'
states_shp = shpreader.natural_earth(resolution='110m',
                                     category='cultural', name=shapename)

popdensity = {
    'New Jersey':  438.00,
    'Rhode Island':   387.35,
    'Massachusetts':   312.68,
    'Connecticut':    271.40,
    'Maryland':   209.23,
    'New York':    155.18,
    'Delaware':    154.87,
    'Florida':     114.43,
    'Ohio':  107.05,
    'Pennsylvania':  105.80,
    'Illinois':    86.27,
    'California':  83.85,
    'Virginia':    69.03,
    'Michigan':    67.55,
    'Indiana':    65.46,
    'North Carolina':  63.80,
    'Georgia':     54.59,
    'Tennessee':   53.29,
    'New Hampshire':   53.20,
    'South Carolina':  51.45,
    'Louisiana':   39.61,
    'Kentucky':   39.28,
    'Wisconsin':  38.13,
    'Washington':  34.20,
    'Alabama':     33.84,
    'Missouri':    31.36,
    'Texas':   30.75,
    'West Virginia':   29.00,
    'Vermont':     25.41,
    'Minnesota':  23.86,
    'Mississippi':   23.42,
    'Iowa':  20.22,
    'Arkansas':    19.82,
    'Oklahoma':    19.40,
    'Arizona':     17.43,
    'Colorado':    16.01,
    'Maine':  15.95,
    'Oregon':  13.76,
    'Kansas':  12.69,
    'Utah':  10.50,
    'Nebraska':    8.60,
    'Nevada':  7.03,
    'Idaho':   6.04,
    'New Mexico':  5.79,
    'South Dakota':  3.84,
    'North Dakota':  3.59,
    'Montana':     2.39,
    'Wyoming':      1.96}

ax.background_patch.set_visible(False)
ax.outline_patch.set_visible(False)

ax.set_title('State Population Density')

for state in shpreader.Reader(states_shp).geometries():

    ### I need to replace the following code with code that sets the
    ### facecolor as a gradient based on the population density above
    facecolor = [0.9375, 0.9375, 0.859375]
    edgecolor = 'black'

    ax.add_geometries([state], ccrs.PlateCarree(),
                      facecolor=facecolor, edgecolor=edgecolor)

plt.show()

【问题讨论】:

    标签: cartopy


    【解决方案1】:

    要访问状态的属性,您需要遍历 .records() 而不是 .geometries()。这是基于您的工作代码。阅读我添加/修改的代码部分中的 cmets 以进行澄清。

    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    import cartopy.io.shapereader as shpreader
    
    fig = plt.figure()
    ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
    
    ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())
    
    shapename = 'admin_1_states_provinces_lakes_shp'
    states_shp = shpreader.natural_earth(resolution='110m',
                                         category='cultural', name=shapename)
    
    popdensity = {
        'New Jersey':  438.00,
        'Rhode Island':   387.35,
        'Massachusetts':   312.68,
        'Connecticut':    271.40,
        'Maryland':   209.23,
        'New York':    155.18,
        'Delaware':    154.87,
        'Florida':     114.43,
        'Ohio':  107.05,
        'Pennsylvania':  105.80,
        'Illinois':    86.27,
        'California':  83.85,
        'Virginia':    69.03,
        'Michigan':    67.55,
        'Indiana':    65.46,
        'North Carolina':  63.80,
        'Georgia':     54.59,
        'Tennessee':   53.29,
        'New Hampshire':   53.20,
        'South Carolina':  51.45,
        'Louisiana':   39.61,
        'Kentucky':   39.28,
        'Wisconsin':  38.13,
        'Washington':  34.20,
        'Alabama':     33.84,
        'Missouri':    31.36,
        'Texas':   30.75,
        'West Virginia':   29.00,
        'Vermont':     25.41,
        'Minnesota':  23.86,
        'Mississippi':   23.42,
        'Iowa':  20.22,
        'Arkansas':    19.82,
        'Oklahoma':    19.40,
        'Arizona':     17.43,
        'Colorado':    16.01,
        'Maine':  15.95,
        'Oregon':  13.76,
        'Kansas':  12.69,
        'Utah':  10.50,
        'Nebraska':    8.60,
        'Nevada':  7.03,
        'Idaho':   6.04,
        'New Mexico':  5.79,
        'South Dakota':  3.84,
        'North Dakota':  3.59,
        'Montana':     2.39,
        'Wyoming':      1.96}
    
    ax.background_patch.set_visible(False)
    ax.outline_patch.set_visible(False)
    
    ax.set_title('State Population Density')
    
    #for state in shpreader.Reader(states_shp).geometries():
    for astate in shpreader.Reader(states_shp).records():
    
        ### You want to replace the following code with code that sets the
        ### facecolor as a gradient based on the population density above
        #facecolor = [0.9375, 0.9375, 0.859375]
    
        edgecolor = 'black'
    
        try:
            # use the name of this state to get pop_density
            state_dens = popdensity[ astate.attributes['name'] ]
        except:
            state_dens = 0
    
        # simple scheme to assign color to each state
        if state_dens < 40:
            facecolor = "lightyellow"
        elif state_dens > 200:
            facecolor = "red"
        else:
            facecolor = "pink"
    
        # `astate.geometry` is the polygon to plot
        ax.add_geometries([astate.geometry], ccrs.PlateCarree(),
                          facecolor=facecolor, edgecolor=edgecolor)
    
    plt.show()
    

    结果图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2011-08-09
      • 2021-08-22
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多