I'm looking to plot a choropleth with a divergent colormap. I need to center the colormap in such a way that the middle color is displayed for a specific value (e.g. 0). How could I do this in GeoPandas/Geoplot?

I tried using a normalizer with geopandas.plot() but it's values are reset to data min and max once I plot:

import geopandas as gpd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.loc[:,'random'] = np.random.normal(size=len(world))+2

norm = mpl.colors.Normalize(-4,4,clip=True)

fig, ax = plt.subplots()
world.plot('random', cmap=cmap, legend=True, norm=norm, ax=ax)

And here is the figure:

noncentered colors


You could normalize the color by using the DivergingNorm function in matplotlib.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import geopandas as gpd

# generate data
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
gdf = gdf[gdf.continent == 'Africa']
gdf['random'] = np.random.gamma(2, 2, len(gdf)) - 2

# normalize color
vmin, vmax, vcenter = gdf.random.min(), gdf.random.max(), 0
divnorm = colors.DivergingNorm(vmin=vmin, vcenter=vcenter, vmax=vmax)
# create a normalized colorbar
cbar = plt.cm.ScalarMappable(norm=divnorm, cmap='RdBu')

# plot
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 7))
# with no normalization
gdf.plot(column='random', cmap='RdBu', legend=True, ax=ax1)
# with normalization
gdf.plot(column='random', cmap='RdBu', legend=False, norm=divnorm, ax=ax2)
# add colorbar
fig.colorbar(cbar, ax=ax2)

enter image description here


  1. You could change vcenter to other desired values.
  2. You need to normalize the color for the map and create a new normalized colorbar. Otherwise, they won't match with one another.
  3. Set legend=False for the normalized map so that the unnormalized colorbar won't be plotted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.