问题描述
我有一个带有差异"列的GeoDataFrame
,该列存储两个数字之间的差值.有时这些数字为正,负或零(如果没有差异).我需要制作一个Choropleth贴图,该贴图具有一些发散的颜色图,其中0(或一些中间缓冲区)作为中点和非对称的颜色条,例如[-0.02, -0.01, 0., 0.01, 0.02, 0.03
].我已经尝试过
I have a GeoDataFrame
with a "differences" column that stores the delta between two numbers. 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
]. 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)
,还将norm
设置为一些精选值:
and also setting norm
to some handpicked values:
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).
所以我的问题是:您将如何使用geopandas.GeoDataFrame.plot()
绘制具有不同比例的地图?您将使用哪种mapclassify
方法?
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?
推荐答案
我可能不理解这个问题,但是两种想法都可以正常工作.例如:
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()
这篇关于如何绘制“差异"?使用geopandas进行地图和mapclassify?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!