简体   繁体   English

如何使用geopandas和mapclassify绘制“差异”地图?

[英]How do I plot a “differences” map using geopandas and mapclassify?

I have a GeoDataFrame with a "differences" column that stores the delta between two numbers. 我有一个带有“差异”列的GeoDataFrame ,该列存储两个数字之间的差异。 Sometimes these numbers are positive, negative or zero if there is not difference. 有时这些数字为正,负或零(如果没有差异)。 I need to make a choropleth map that has some divergent colormap with 0 (or some middle buffer) as the midpoint and non-symetrical colorbar, for example [ -0.02, -0.01, 0., 0.01, 0.02, 0.03 ]. 我需要制作一个色散图,其色散图以0(或一些中间缓冲区)为中点,且非对称色条,例如[ -0.02, -0.01, 0., 0.01, 0.02, 0.03 ]。 I've experimented with 我尝试过

class MidPointNormalize(mp.colors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        mp.colors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y), np.isnan(value))

norm = MidPointNormalize(midpoint=0,vmin=diff_merge_co["diff"].min(),
                         vmax=diff_merge_co["diff"].max())
diff_merge_co.plot(ax=ax, column="diff", cmap="coolwarm", norm=norm,
                   legend=True)

and also setting norm to some handpicked values: 并将norm设置为一些精选值:

bounds = np.array([-0.02, -0.01, 0., 0.01, 0.02, 0.03])
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256) 

but there are a lot of technical problems with that (including that the colorbar is not persisted upwards, and the obvious ugliness of handpicking your values). 但与此同时存在很多技术问题(包括色标无法持续显示,以及手工挑选您的价值观时显然很丑陋)。

So my question is: how would you draw a map with a divergent scale using geopandas.GeoDataFrame.plot() and which mapclassify method would you use? 所以我的问题是:如何使用geopandas.GeoDataFrame.plot()绘制具有不同比例的地图?您将使用哪种mapclassify方法?

I may not understand the issue, but both ideas should work fine. 我可能不理解这个问题,但是两种想法都可以正常工作。 For example: 例如:

import geopandas as gpd
print(gpd.__version__)   ## 0.4.1
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt 
import matplotlib.colors as mcolors

class MidPointNormalize(mcolors.Normalize):
    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
        self.midpoint = midpoint
        mcolors.Normalize.__init__(self, vmin, vmax, clip)

    def __call__(self, value, clip=None):
        x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
        return np.ma.masked_array(np.interp(value, x, y), np.isnan(value))

## Some data file from ## http://biogeo.ucdavis.edu/data/diva/adm/USA_adm.zip    
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) 
quant = np.random.rand(len(gdf))*0.05-0.02
gdf['quant']=quant
print(gdf.head())


fig, (ax, ax2) = plt.subplots(2, figsize=(7,6))

norm=MidPointNormalize(-0.02, 0.03, 0)
gdf.plot(column='quant', cmap='RdBu', norm=norm, ax=ax)
fig.colorbar(ax.collections[0], ax=ax)


bounds = np.array([-0.02, -0.01, 0., 0.01, 0.02, 0.03])
norm2 = mcolors.BoundaryNorm(boundaries=bounds, ncolors=256) 
gdf.plot(column='quant', cmap='RdBu', norm=norm2, ax=ax2)
fig.colorbar(ax2.collections[0], ax=ax2)


plt.show()

在此处输入图片说明

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

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