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

*爱你&永不变心* 提交于 2020-02-25 05:54:24

问题


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)

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

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?


回答1:


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



来源:https://stackoverflow.com/questions/55679885/how-do-i-plot-a-differences-map-using-geopandas-and-mapclassify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!