GeoPandas, MatPlotLib Plot Custom Colors

前端 未结 1 741
说谎
说谎 2021-02-09 16:21

Given the shape file available here: I\'d like to plot the specified set of counties below with custom colors; \'blue\' for Wayne and Washtenaw counties and \'grey\' for the oth

相关标签:
1条回答
  • 2021-02-09 16:43

    Geopandas wants to color your map according to data in your geopandas dataframe. So the simplest coloring scheme you could go with is to add a column 'color' to your dataframe and populate it with some values based on how you want your counties colored.

    import numpy as np
    import geopandas as gpd
    import matplotlib.pyplot as plt
    from matplotlib.colors import LinearSegmentedColormap
    
    shpfile = 'cb_2015_us_county_20m.shp'
    c = gpd.read_file(shpfile)
    c = c.loc[c['GEOID'].isin(['26161','26093','26049','26091','26075',
                               '26125','26163','26099','26115','26065'])]
    
    c['color'] = np.zeros(len(c))
    # 23 is index for Washtenaw county and 1992 is index for Wayne county
    c.ix[23, 'color'] = 1.0
    c.ix[1992, 'color'] = 1.0
    
    # create simple linear colormap that maps grey to blue
    cmap = LinearSegmentedColormap.from_list(
        'mycmap', [(0, 'grey'), (1, 'blue')])
    
    c.plot(column='color', cmap=cmap)
    

    Perhaps it's not the most elegant solution, but this should at least explain the concept of how colormaps function in geopandas and get you the plot you're looking for. Also check out this page of the geopandas docs for a little more info on map coloring.

    0 讨论(0)
提交回复
热议问题