When I try to use the legend_kwds argument to change the font size of my colorbar, I keep getting this error:

TypeError: init() got an unexpected keyword argument 'fontsize'

ax = df.plot(figsize=(20,16), alpha=0.8, column='value', legend=True, cmap='OrRd', legend_kwds={'fontsize':20})


Does anyone know how I can increase the font size of the colorbar with GeoPandas? I can't seem to find a keyword that works. I'm using GeoPandas 0.8.1 and Matplotlib 3.3.1.

  • Can you add a screenshot of the plot – BERA Nov 5 at 7:32
  • So if I omit "legend_kwds={'fontsize':20}" the plot outputs normally it's just that there appears to be a bug in GeoPandas that prevents this argument from working, the plot doesn't really help contribute much to debugging. – Jack Simpson Nov 5 at 12:26
  • I thought you by colorbar meant bar plot but I understand now from the answer below. – BERA Nov 5 at 14:41

It is a matplotlib problem and not a GeoPandas problem.

See the solution of Seb Bacon: geopandas-legend-font-size.ipynb

As the Jupyter notebook has problems (error opening it), I describe it here.

The legend is actually a Colorbar object. To change the legend's font size, we have to get hold of the Colorbar's Axes object, and call .tick_params() on that.

import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) 
world = world[(world.pop_est>0) & (world.name!="Antarctica")] 
world['gdp_per_cap'] = world.gdp_md_est / world.pop_est

First, we call the .plot() method as usual, but we capture the return value which is the Axes of the map.

ax = world.plot(column='gdp_per_cap', cmap='OrRd', legend=True)

We can use this to get hold of the Figure that contains the Axes.

fig = ax.figure

The Figure has two Axes: one for the map, and one for the Colorbar. The one we care about is the second one.

cb_ax = fig.axes[1] 

We can now set the font size for the Colorbar. Other parameters for tick_params are documented at: matplotlib.axes.Axes.tick_params


enter image description here


enter image description here

| improve this answer | |

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.