简体   繁体   English

matplotlib geopandas绘制带有设置容器的叶绿素的colorcheme

[英]matplotlib geopandas plot chloropleth with set bins for colorscheme

How do I set a consistent colorscheme for three axes in the same figure? 如何为同一图中的三个axes设置一致的颜色方案?

The following should be a wholly reproducible example to run the code and get the same figure I have posted below. 以下应该是一个完全可复制的示例,用于运行代码并获得与我在下面发布的相同的图。

Get the shapefile data from the Office for National Statistics . 国家统计局获取shapefile数据。 Run this in a terminal as a bash file / commands. 作为bash文件/命令在终端中运行它。

wget --output-document 'LA_authorities_boundaries.zip' 'https://opendata.arcgis.com/datasets/8edafbe3276d4b56aec60991cbddda50_1.zip?outSR=%7B%22latestWkid%22%3A27700%2C%22wkid%22%3A27700%7D&session=850489311.1553456889'

mkdir LA_authorities_boundaries
cd LA_authorities_boundaries
unzip ../LA_authorities_boundaries.zip

The python code that reads the shapefile and creates a dummy GeoDataFrame for reproducing the behaviour. 读取shapefile并创建一个虚拟GeoDataFrame来重现行为的python代码。

import geopandas as gpd
import pandas as pd
import matplotlib.pyplot as plt

gdf = gpd.read_file(
    'LA_authorities_boundaries/Local_Authority_Districts_December_2015_Full_Extent_Boundaries_in_Great_Britain.shp'
)

# 380 values
df = pd.DataFrame([])
df['AREA_CODE'] = gdf.lad15cd.values
df['central_pop'] = np.random.normal(30, 15, size=(len(gdf.lad15cd.values)))
df['low_pop'] = np.random.normal(10, 15, size=(len(gdf.lad15cd.values)))
df['high_pop'] = np.random.normal(50, 15, size=(len(gdf.lad15cd.values)))

Join the shapefile from ONS and create a geopandas.GeoDataFrame 从ONS加入shapefile并创建geopandas.GeoDataFrame

def join_df_to_shp(pd_df, gpd_gdf):
    """"""
    df_ = pd.merge(pd_df, gpd_gdf[['lad15cd','geometry']], left_on='AREA_CODE', right_on='lad15cd', how='left')

    # DROP the NI counties
    df_ = df_.dropna(subset=['geometry'])

    # convert back to a geopandas object (for ease of plotting etc.)
    crs = {'init': 'epsg:4326'}
    gdf_ = gpd.GeoDataFrame(df_, crs=crs, geometry='geometry')
    # remove the extra area_code column joined from gdf
    gdf_.drop('lad15cd',axis=1, inplace=True)

    return gdf_

pop_gdf = join_df_to_shp(df, gdf)

Make the plots 绘制情节

fig,(ax1,ax2,ax3,) = plt.subplots(1,3,figsize=(15,6))

pop_gdf.plot(
    column='low_pop', ax=ax1, legend=True,  scheme='quantiles', cmap='OrRd',
)
pop_gdf.plot(
    column='central_pop', ax=ax2, legend=True, scheme='quantiles', cmap='OrRd',
)
pop_gdf.plot(
    column='high_pop', ax=ax3, legend=True,  scheme='quantiles', cmap='OrRd',
)
for ax in (ax1,ax2,ax3,):
    ax.axis('off')

在此处输入图片说明

I want all three ax objects to share the same bins (preferable the central_pop scenario quantiles ) so that the legend is consistent for the whole figure. 我希望所有三个ax对象共享相同的垃圾箱(最好是central_pop场景quantiles ),以便图例对于整个图形是一致的。 The quantiles from ONE scenario (central) would become the levels for all quantiles从一个场景(中心)将成为该levels的所有

This way I should see darker colors (more red) in the far right ax showing the high_pop scenario. 这样,我应该在显示high_pop场景的最右端的ax看到较深的颜色(更多的红色)。

How can I set the colorscheme bins for the whole figure / each of the ax objects? 如何为整个图形/每个ax对象设置colorscheme bin?

The simplest way I can see this working is either a) Provide a set of bins to the geopandas.plot() function b) extract the colorscheme / bins from one ax and apply it to another. 我可以看到此工作的最简单方法是:a)向geopandas.plot()函数提供一组垃圾箱b)从一个ax提取geopandas.plot() / bins并将其应用于另一个ax

From geopandas 0.5 onwards you can use a custom scheme defined as scheme="User_Defined" and supply the binning via classification_kwds . 从geopandas 0.5起,可以使用定义为自定义方案scheme="User_Defined" ,并且经由提供合并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, eg via 这应该很容易做到,例如通过

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

then setting classification_kwds=dict(bins=bins) in the above code. 然后在上面的代码中设置classification_kwds=dict(bins=bins)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM