I'm looking for a way to format/round the numerical legend labels in those maps produced by .plot()
function in GeoPandas. For example:
gdf.plot(column='pop2010', scheme='QUANTILES', k=4)
This gives me a legend with many decimal places:
I want the legend label to be integers.
As I recently encountered the same issue, and a solution does not appear to be readily available on Stack Overflow or other sites, I thought I would post the approach I took in case it is useful.
First, a basic plot using the
geopandas
world map:The method I used relied on the
get_texts()
method for thematplotlib.legend.Legend
object, then iterating over the items inleg.get_texts()
, splitting the text element into the lower and upper bounds, and then creating a new string with formatting applied and setting this with theset_text()
method.This is very much a 'trial and error' approach, so I wouldn't be surprised if there were a better way. Still, perhaps this will be helpful.