我有一个带有“差异”列的GeoDataFrame
,该列存储两个数字之间的差值。有时这些数字为正,负或零(如果没有差异)。我需要制作一个choropleth映射,该映射具有一些发散的颜色图,其中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
方法?
答案 0 :(得分: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('data/USA_adm/USA_adm1.shp')
quant = np.random.rand(52)*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)
ax.set_xlim(-180,-60)
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)
ax2.set_xlim(-180,-60)
plt.show()