Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Bug summary
When adding legend to an axes withloc
set to"best"
, the created legend does not try to avoid line collections added viaadd_collection
.
It seems_auto_legend_data
only returns the offsets of a collection, so only scatter-plot-like collections are avoided by the legend. Line collections that are intended to draw data lines always have zero orNone
offset values and are thus not avoided when creating the legend.
Code for reproduction
importmatplotlib.pyplotaspltimportmatplotlib.collectionsfig,ax=plt.subplots(1,1,figsize=(7,4),constrained_layout=True)lc=matplotlib.collections.LineCollection([[(0,0), (1,1)]],label='Foo')ax.add_collection(lc)ax.legend()plt.show()
Actual outcome
Expected outcome
Additional information
I work this around by using a patch that essentially adds the following code after the branch here:
matplotlib/lib/matplotlib/legend.py
Line 839 ind2f87e8
elifisinstance(artist,Patch): |
elif ( isinstance(artist, Collection) and ( artist.get_offsets() is None or (np.array(artist.get_offsets()) == 0).all() ) ): transform, _, _, paths = artist._prepare_points() lines += [transform.transform_path(path) for path in paths]
But I'm not familar enough with the code to say this won't create other problems, and it seems to be extremely slow with large line collections.
Operating system
No response
Matplotlib Version
3.5.0
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
No response