René's URL Explorer Experiment


Title: Add legend support for PatchCollection by FazeelUsmani · Pull Request #30756 · matplotlib/matplotlib · GitHub

Open Graph Title: Add legend support for PatchCollection by FazeelUsmani · Pull Request #30756 · matplotlib/matplotlib

X Title: Add legend support for PatchCollection by FazeelUsmani · Pull Request #30756 · matplotlib/matplotlib

Description: PR summary Closes #23998 Why is this change necessary? PatchCollection instances do not currently display in legends even when assigned a label. This has been a long-standing limitation that forces users to create manual legend entries, making the API inconsistent with other collection types like PolyCollection and PathCollection. What problem does it solve? Before this fix, the following code would not display a legend entry: import matplotlib.pyplot as plt from matplotlib.collections import PatchCollection from matplotlib.patches import Polygon fig, ax = plt.subplots() p1 = Polygon([[0, 0], [100, 100], [200, 0]]) p2 = Polygon([[400, 0], [500, 100], [600, 0]]) pc = PatchCollection([p1, p2], label="my patches", facecolor='blue') ax.add_collection(pc) ax.legend() # Legend was empty ❌ plt.show() After this fix, the legend correctly displays the "my patches" label with a rectangular swatch matching the collection's visual properties. Implementation reasoning The implementation adds a new HandlerPatchCollection class that inherits from the existing HandlerPolyCollection. This approach: Eliminates code duplication: Both PatchCollection and PolyCollection inherit from Collection and share identical APIs for accessing colors, line styles, and other visual properties Maintains consistency: Uses the same legend representation (Rectangle) as PolyCollection Ensures maintainability: Bug fixes or enhancements to HandlerPolyCollection automatically benefit HandlerPatchCollection The handler extracts visual properties (face color, edge color, line width, line style) from the first patch in the collection and creates a rectangular legend entry. Minimum self-contained example import matplotlib.pyplot as plt import matplotlib.patches as mpatches from matplotlib.collections import PatchCollection fig, ax = plt.subplots() # Create various patches patches = [ mpatches.Circle((0.2, 0.5), 0.1), mpatches.Rectangle((0.5, 0.3), 0.2, 0.3), ] # Create PatchCollection with label pc = PatchCollection(patches, facecolor='red', edgecolor='black', linewidths=2, label='My patches') ax.add_collection(pc) ax.autoscale_view() # Legend now works! ax.legend() plt.show() PR checklist "closes #23998" is in the body of the PR description to link the related issue New and changed code is tested Added 5 comprehensive tests covering: Basic functionality Visual property preservation match_original=True parameter Empty collection edge case Visual regression test with @image_comparison [N/A] Plotting related features are demonstrated in an example This is a bug fix for existing functionality, not a new plotting feature Code examples are included in the What's New entry New Features and API Changes are noted with a directive and release note Added doc/release/next_whats_new/patchcollection_legend.rst Includes user-facing description and code example Documentation complies with general and docstring guidelines HandlerPatchCollection includes comprehensive docstring Follows matplotlib's docstring conventions Includes "See Also" section

Open Graph Description: PR summary Closes #23998 Why is this change necessary? PatchCollection instances do not currently display in legends even when assigned a label. This has been a long-standing limitation that forces...

X Description: PR summary Closes #23998 Why is this change necessary? PatchCollection instances do not currently display in legends even when assigned a label. This has been a long-standing limitation that forces...

Opengraph URL: https://github.com/matplotlib/matplotlib/pull/30756

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:2cbfefcb-863a-afb9-6c4c-b481a2fd814e
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-id882E:141FAF:9B43A8:D830F6:6A4E7E4A
html-safe-nonce0d89cc1ce7fd6cfa2eb18ae88130acd62ca7561b52e4f9ad587b1f7d9bb29c15
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4ODJFOjE0MUZBRjo5QjQzQTg6RDgzMEY2OjZBNEU3RTRBIiwidmlzaXRvcl9pZCI6IjkwNzc4MjU5MjIwNzgyNDQ0MjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacd90d943154e710de7531e997c70f416f8310171c21a577b1d572d5fd95ed660f
hovercard-subject-tagpull_request:3017531813
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/matplotlib/matplotlib/pull/30756/files
twitter:imagehttps://avatars.githubusercontent.com/u/8823157?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/8823157?s=400&v=4
og:image:altPR summary Closes #23998 Why is this change necessary? PatchCollection instances do not currently display in legends even when assigned a label. This has been a long-standing limitation that forces...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None41b6ab3ba6d20a71766ac245b5a4a94c6fc672a9cd4da7d44c1b33ab8bf6a21c
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git
octolytics-dimension-user_id215947
octolytics-dimension-user_loginmatplotlib
octolytics-dimension-repository_id1385122
octolytics-dimension-repository_nwomatplotlib/matplotlib
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id1385122
octolytics-dimension-repository_network_root_nwomatplotlib/matplotlib
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasee6a744804e8e70f97b4d5a18a94dcc63db22f97a
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/matplotlib/matplotlib/pull/30756/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fpull%2F30756%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fmatplotlib%2Fmatplotlib%2Fpull%2F30756%2Ffiles
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=matplotlib%2Fmatplotlib
Reloadhttps://github.com/matplotlib/matplotlib/pull/30756/files
Reloadhttps://github.com/matplotlib/matplotlib/pull/30756/files
Reloadhttps://github.com/matplotlib/matplotlib/pull/30756/files
Please reload this pagehttps://github.com/matplotlib/matplotlib/pull/30756/files
matplotlib https://github.com/matplotlib
matplotlibhttps://github.com/matplotlib/matplotlib
Please reload this pagehttps://github.com/matplotlib/matplotlib/pull/30756/files
Notifications https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Fork 8.4k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Star 23k https://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib
Code https://github.com/matplotlib/matplotlib
Issues 1.1k https://github.com/matplotlib/matplotlib/issues
Pull requests 410 https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality 0 https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
Code https://github.com/matplotlib/matplotlib
Issues https://github.com/matplotlib/matplotlib/issues
Pull requests https://github.com/matplotlib/matplotlib/pulls
Actions https://github.com/matplotlib/matplotlib/actions
Projects https://github.com/matplotlib/matplotlib/projects
Wiki https://github.com/matplotlib/matplotlib/wiki
Security and quality https://github.com/matplotlib/matplotlib/security
Insights https://github.com/matplotlib/matplotlib/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fmatplotlib%2Fmatplotlib%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fmatplotlib%2Fmatplotlib%2Fissues%2Fnew%2Fchoose
story645https://github.com/story645
matplotlib:mainhttps://github.com/matplotlib/matplotlib/tree/main
FazeelUsmani:fix-patchcollection-legendhttps://github.com/FazeelUsmani/matplotlib/tree/fix-patchcollection-legend
Conversation 32 https://github.com/matplotlib/matplotlib/pull/30756
Commits 18 https://github.com/matplotlib/matplotlib/pull/30756/commits
Checks 36 https://github.com/matplotlib/matplotlib/pull/30756/checks
Files changed https://github.com/matplotlib/matplotlib/pull/30756/files
Please reload this pagehttps://github.com/matplotlib/matplotlib/pull/30756/files
Add legend support for PatchCollection https://github.com/matplotlib/matplotlib/pull/30756/files#top
Show all changes 18 commits https://github.com/matplotlib/matplotlib/pull/30756/files
0f13209 Add support for PatchCollection legends and update documentation FazeelUsmani Nov 17, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/0f13209076c905800159b328d27f69d4ecd9d7ef
3f013f6 Add type stub for HandlerPatchCollection FazeelUsmani Nov 17, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/3f013f62e111d499ccc8cfd0a85b4e3df60b27eb
567b478 Remove visual regression test (baseline image needed) FazeelUsmani Nov 17, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/567b478c3af62b3d8277e4274f87909f226a0fe5
8fc57eb Trigger CI re-run FazeelUsmani Nov 17, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/8fc57eb516459be9be10276c971b66f82c6141c6
478de45 Add explicit figure cleanup in PatchCollection legend tests FazeelUsmani Nov 17, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/478de4522aef189337353fa8e8debd4bc06c576f
8536bfc Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/8536bfca7998ea3a47af99673b5cd159be6800bd
71c9a86 Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/71c9a866e92a2856f1ae1fbb27763a2182c729d9
618d426 Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/618d426cf387108a11eb9319ff19181e6076b935
122a7d6 Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/122a7d638145bb49ca63e6451087d172dbc811b9
83adf5c Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/83adf5ce86aa1af65780c4fa23513a3881222a66
477bf97 Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/477bf9705a3e6318a6edb164665c516c4a66a010
1a1b20a Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/1a1b20aa096d450dd48f2553866959ea5e661169
bd2f926 Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/bd2f926ce0ada6367eb855ea52678cac23cdb64b
33242ca Update lib/matplotlib/tests/test_legend.py FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/33242cacc51c0d53e3a062c642fb1059b1290238
b669f4d Remove redundant test_patchcollection_legend_match_original test FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/b669f4d9409a64e2035d0b18ff04db1547b68fda
92aaeca Test behavior instead of implementation details FazeelUsmani Nov 18, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/92aaecaa3ffc925d52a76a424be8a5fbec0ff791
555faa6 Address story645's review: simplify implementation and improve tests FazeelUsmani Nov 20, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/555faa660d1ee864f0202aa9dda9960be589059b
84a911e Update documentation and tests for PatchCollection legend support FazeelUsmani Nov 24, 2025 https://github.com/matplotlib/matplotlib/pull/30756/commits/84a911ed2de902704bfbaef6434908b534d937da
Clear filters https://github.com/matplotlib/matplotlib/pull/30756/files
Please reload this pagehttps://github.com/matplotlib/matplotlib/pull/30756/files
Please reload this pagehttps://github.com/matplotlib/matplotlib/pull/30756/files
patchcollection_legend.rst https://github.com/matplotlib/matplotlib/pull/30756/files#diff-08deaf904d41991799789536cd418da417340e2b49c4aeb1e7dfa240cfbc27f4
legend.py https://github.com/matplotlib/matplotlib/pull/30756/files#diff-e7a00bb4a353fa623c1d0c1de5c6ca602b7ab38e8906d4842640983133b9196a
test_legend.py https://github.com/matplotlib/matplotlib/pull/30756/files#diff-1b573023d84935306a147bec7f137e59ae79c3d4f3bbea81152eb0c94b58a579
doc/release/next_whats_new/patchcollection_legend.rsthttps://github.com/matplotlib/matplotlib/pull/30756/files#diff-08deaf904d41991799789536cd418da417340e2b49c4aeb1e7dfa240cfbc27f4
View file https://github.com/FazeelUsmani/matplotlib/blob/84a911ed2de902704bfbaef6434908b534d937da/doc/release/next_whats_new/patchcollection_legend.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/matplotlib/matplotlib/pull/30756/{{ revealButtonHref }}
lib/matplotlib/legend.pyhttps://github.com/matplotlib/matplotlib/pull/30756/files#diff-e7a00bb4a353fa623c1d0c1de5c6ca602b7ab38e8906d4842640983133b9196a
View file https://github.com/FazeelUsmani/matplotlib/blob/84a911ed2de902704bfbaef6434908b534d937da/lib/matplotlib/legend.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/matplotlib/matplotlib/pull/30756/{{ revealButtonHref }}
https://github.com/matplotlib/matplotlib/pull/30756/files#diff-e7a00bb4a353fa623c1d0c1de5c6ca602b7ab38e8906d4842640983133b9196a
https://github.com/matplotlib/matplotlib/pull/30756/files#diff-e7a00bb4a353fa623c1d0c1de5c6ca602b7ab38e8906d4842640983133b9196a
https://github.com/matplotlib/matplotlib/pull/30756/files#diff-e7a00bb4a353fa623c1d0c1de5c6ca602b7ab38e8906d4842640983133b9196a
https://github.com/matplotlib/matplotlib/pull/30756/files#diff-e7a00bb4a353fa623c1d0c1de5c6ca602b7ab38e8906d4842640983133b9196a
lib/matplotlib/tests/test_legend.pyhttps://github.com/matplotlib/matplotlib/pull/30756/files#diff-1b573023d84935306a147bec7f137e59ae79c3d4f3bbea81152eb0c94b58a579
View file https://github.com/FazeelUsmani/matplotlib/blob/84a911ed2de902704bfbaef6434908b534d937da/lib/matplotlib/tests/test_legend.py
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/matplotlib/matplotlib/pull/30756/{{ revealButtonHref }}
https://github.com/matplotlib/matplotlib/pull/30756/files#diff-1b573023d84935306a147bec7f137e59ae79c3d4f3bbea81152eb0c94b58a579
Please reload this pagehttps://github.com/matplotlib/matplotlib/pull/30756/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.