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

问题描述 投票:0回答:1

我有一个带有“差异”列的GeoDataFrame,它存储两个数字之间的差值。如果没有差异,有时这些数字是正数,负数或零。我需要制作一个具有一些发散色图的等值区域图,其中0(或一些中间缓冲区)作为中点和非对称色条,例如[-0.02, -0.01, 0., 0.01, 0.02, 0.03]。我试过了

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)

并将norm设置为一些精心挑选的值:

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

但是存在很多技术问题(包括颜色栏没有持续向上,以及挑选你的价值的明显丑陋)。

所以我的问题是:你如何使用geopandas.GeoDataFrame.plot()绘制具有不同比例的地图,你会使用哪种mapclassify方法?

python matplotlib gis geopandas
1个回答
1
投票

我可能不理解这个问题,但这两个想法应该可以正常工作。例如:

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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.