Polygon handles not passing to legend
See original GitHub issueI am trying to plot some polygons and lines from geopandas dataframe and add a legend. However, when plotting the polygons, the labels don’t get passed to the legend handles.
So for example
fig,ax=plt.subplots()
lineshp.plot(ax=ax,color='k', linewidth=3,label='line')
polyshp.plot(ax=ax,color='r',label='poly')
ax.get_legend_handles_labels()
It returns: ([<matplotlib.collections.LineCollection at 0x116681d0>], [‘line’]) and does not include the polygon handles.
Anyone has an idea of how I could fix this? This seems to be a new issue for me.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:6 (3 by maintainers)
Top Results From Across the Web
python - Error `No handles with labels` when trying to relocate ...
(I am plotting a GeoDataFrame.) No handles with labels found to put in legend. (I suspect if it has something to do with...
Read more >Legends in python | Andrew Wheeler
You can think of handles as just points/lines/polygons that refer to individual parts of the legend. And so combining the lines and polygons...
Read more >Adding custom names to a GeoPandas legend
The legend comes up empty and I get a message that No handles with labels found to put in legend .
Read more >matplotlib.pyplot.legend — Matplotlib 3.6.2 documentation
legend () legend(handles, labels) legend(handles=handles) legend(labels) ... be added to the legend are automatically determined, when you do not pass in any ...
Read more >Customize Map Legends and Colors in Python using Matplotlib
It looks like you have some missing values in your road types. You want to plot all road types even those that are...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’m using geopandas 0.8.1, and I also faced this problem. Here is my solution
Although this approach can fulfill my requirements, it’s really cumbersome, and I really hope that geopandas can fix it.
OK, this is a ‘limitation’ of matplotlib. They only support automatically creating legend handlers for some collections, so for points and lines they do, but not for PatchCollections (polygons). So we could in principle, if a label is passed, create ourselves a legend handle, and pass that to matplotlib.
See https://matplotlib.org/2.0.0/users/legend_guide.html