Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add HandlerPatchCollection to support legends for PatchCollection #29952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

anika3943
Copy link

Summary

This PR adds a default legend handler for matplotlib.collections.PatchCollection, enabling automatic legend support without the need for manual proxy artists.

Motivation

As discussed in issue #23998, currently PatchCollection objects do not appear in legends because they lack a registered legend handler. This change introduces a HandlerPatchCollection that extracts visual properties from the first patch in the collection and uses them to generate a representative patch for the legend.

Implementation

  • Introduced HandlerPatchCollection (subclassing HandlerPatch) that mirrors HandlerPolyCollection.
  • Registered the handler in _default_handler_map to enable automatic use.

Related Issues

Fixes #23998
Closes #24028

Example

from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
p1 = Polygon([[0, 0], [1, 1], [2, 0]])
p2 = Polygon([[3, 0], [4, 1], [5, 0]])
coll = PatchCollection([p1, p2], label='Polygons', facecolor='cyan')
ax.add_collection(coll)
ax.set_xlim(0, 6)
ax.set_ylim(0, 2)
ax.legend()
plt.show()

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Copy link
Member

@rcomer rcomer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @anika3943 for the potential contribution. However, the failing checks below show that your implementation is broken. Did you run the example code you posted to see if it works?

For new features, we also require several other changes in addition to the main code change. Please see the checklist here. A lot of that work is already done in #24028, and you are very welcome to re-use those changes if they seem correct.

Finally, I see you have used your main branch, which we recommend against. So, instead of continuing here, I suggest starting again with a new branch and making a new pull request when that is ready. You may find the steps set out at Start a Pull Request useful.

So I will close this one here. Note we also have the incubator gitter channel for new people to get help if you need it.

@@ -810,3 +810,42 @@ def create_artists(self, legend, orig_handle,
self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
return [p]

from matplotlib.legend_handler import HandlerPatchCollection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is legend_handler, so you do not need to import from it.

@rcomer rcomer closed this Apr 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Labels for PatchCollection do not show
2 participants