Search code examples
plotgeopandaslegend-properties

Custom legend labels - geopandas.plot()


A colleague and I have been trying to set custom legend labels, but so far have failed. Code and details below - any ideas much appreciated!

Notebook: toy example uploaded here

Goal: change default rate values used in the legend to corresponding percentage values

Problem: cannot figure out how to access the legend object or pass legend_kwds to geopandas.GeoDataFrame.plot()

Data: KCMO metro area counties

Excerpts from toy example

Step 1: read data

# imports
import geopandas as gpd
import matplotlib.pyplot as plt
%matplotlib inline
# read data
gdf = gpd.read_file('kcmo_counties.geojson')

Option 1 - get legend from ax as suggested here:

ax = gdf.plot('val', legend=True)
leg = ax.get_legend()
print('legend object type: ' + str(type(leg))) # <class NoneType>
plt.show()

Option 2: pass legend_kwds dictionary - I assume I'm doing something wrong here (and clearly don't fully understand the underlying details), but the _doc_ from Geopandas's plotting.py - for which GeoDataFrame.plot() is simply a wrapper - does not appear to come through...

# create number of tick marks in legend and set location to display them
import numpy as np
numpoints = 5
leg_ticks = np.linspace(-1,1,numpoints)

# create labels based on number of tickmarks
leg_min = gdf['val'].min()
leg_max = gdf['val'].max()
leg_tick_labels = [str(round(x*100,1))+'%' for x in np.linspace(leg_min,leg_max,numpoints)]
leg_kwds_dict = {'numpoints': numpoints, 'labels': leg_tick_labels}

# error "Unknown property legend_kwds" when attempting it:
f, ax = plt.subplots(1, figsize=(6,6))
gdf.plot('val', legend=True, ax=ax, legend_kwds=leg_kwds_dict)

UPDATE Just came across this conversation on adding in legend_kwds - and this other bug? which clearly states legend_kwds was not in most recent release of GeoPandas (v0.3.0). Presumably, that means we'll need to compile from the GitHub master source rather than installing with pip/conda...


Solution

  • I've just come across this issue myself. After following your link to the Geopandas source code, it appears that the colourbar is added as a second axis to the figure. so you have to do something like this to access the colourbar labels (assuming you have plotted a chloropleth with legend=True):

    # Get colourbar from second axis
    colourbar = ax.get_figure().get_axes()[1]
    

    Having done this, you can manipulate the labels like this:

    # Get numerical values of yticks, assuming a linear range between vmin and vmax:
    yticks = np.interp(colourbar.get_yticks(), [0,1], [vmin, vmax])
    
    # Apply some function f to each tick, where f can be your percentage conversion
    colourbar.set_yticklabels(['{0:.2f}%'.format(ytick*100) for ytick in yticks])