matplotlib geopandas plot chloropleth with set bins for colorscheme

后端 未结 1 1797
星月不相逢
星月不相逢 2020-12-12 01:59

How do I set a consistent colorscheme for three axes in the same figure?

The following should be a wholly reproducible example to run the code and get

相关标签:
1条回答
  • 2020-12-12 02:40

    From geopandas 0.5 onwards you can use a custom scheme defined as scheme="User_Defined" and supply the binning via classification_kwds.

    import geopandas as gpd
    print(gpd.__version__)   ## 0.5
    import numpy as np; np.random.seed(42)
    import matplotlib.pyplot as plt 
    
    gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) 
    gdf['quant']=np.random.rand(len(gdf))*100-20
    
    fig, ax = plt.subplots()
    
    gdf.plot(column='quant', cmap='RdBu', scheme="User_Defined", 
             legend=True, classification_kwds=dict(bins=[-10,20,30,50,70]),
             ax=ax)
    
    plt.show()
    

    So the remaining task is to get a list of bins from the quantiles of one of the columns. This should be easily done, e.g. via

    import mapclassify
    bins = mapclassify.Quantiles(gdf['quant'], k=5).bins
    

    then setting classification_kwds=dict(bins=bins) in the above code.

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