Skip to content

Labels for PatchCollection do not show #23998

Closed

Description

@Robotatron

Bug summary

  1. Labels / legend is not displayed
  2. Autoscale does nothing, I have to manually set the limits

image

Code for reproduction

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

fig, axs = plt.subplots()
p1, p2 = Polygon([[0,0],[100,100],[200,0]], label="p1"), Polygon([[400,0],[500,100],[600,0]], label="p2")
p = PatchCollection([p1,p2], label="asd")
axs.add_collection(p, autolim=True)
axs.set_xlim(right=600)
axs.set_ylim(top=100)
axs.legend()

Actual outcome

No labels/legend are shown - image

Expected outcome

Legend is shown

Additional information

No response

Operating system

No response

Matplotlib Version

3.5.2

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

No response

Activity

  1. jklymak commented on Sep 26, 2022

    @jklymak
    Member

    legends don't traverse every artist and look for labels. Low-level objects that need something like add_foo usually are not part of the legend. I personally think that is fine - if you have to do add_foo you are doing something relatively low level, and can be expected to manually add the artist to the legend.

  2. timhoffm commented on Sep 26, 2022

    @timhoffm
    Member

    I can see an argument for supporting PatchCollection. We support Line2D, Patch and LineCollection in legends, so why not PatchCollection.

    I think this is a good first issue. What needs to be done:

    • Understand the concept of legend handlers
    • write a PatchCollectionHandler in matplotlib.legend_handlers. As with other collections this should use the properties of the first patch in the collection. Take inspiration from
      • HandlerPatch to see how a Patch is handled
      • HandlerLineCollection to see how a handler for a collection works
    • add the handler to Legend._default_handler_map
  3. added
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Sep 26, 2022
  4. emmamastro commented on Sep 27, 2022

    @emmamastro

    I can try to take this issue!

  5. FarukFS commented on Sep 28, 2022

    @FarukFS

    I have created a draft pull request. However, since this is my first contribution to an open source project, I am not sure on how to document it. I would appreciate some help :)

  6. julia-harbord commented on Sep 28, 2022

    @julia-harbord

    Any way I can help with this? I am new to programming and new to open source so I might need some help. I have worked with matplotlib before very briefly.

  7. FarukFS commented on Sep 28, 2022

    @FarukFS

    Any way I can help with this? I am new to programming and new to open source so I might need some help. I have worked with matplotlib before very briefly.

    I will probably finish these changes tomorrow. I would recommend doing another ticket since I’m almost done. However, I’m also new to open source, if you want, we can learn together? After this issue, I will try to tackle another issues, feel free to reach out to me if I can be of any help :)

  8. julia-harbord commented on Sep 29, 2022

    @julia-harbord

    This might be a long shot, but would you be willing to possibly meet with me virtually and we could work on it together at some point tomorrow? I picked this particular issue for a class assignment at school and I'd really like to contribute to this one in any way I can if it's still possible. No worries either way, I just definitely need some help getting started with open-source :)

  9. emmamastro commented on Sep 29, 2022

    @emmamastro

    This might be a long shot, but would you be willing to possibly meet with me virtually and we could work on it together at some point tomorrow? I picked this particular issue for a class assignment at school and I'd really like to contribute to this one in any way I can if it's still possible. No worries either way, I just definitely need some help getting started with open-source :)

    I am using this for UF's open source assignment as I commented before FarukFS, but noticed he did a pull request first. I am submitting a pull request tonight for the sake of the assignment when I get wifi on my laptop just so you know if you are also trying to use this for that assignment!

  10. julia-harbord commented on Sep 29, 2022

    @julia-harbord

    Oh wow yes I am also trying to use this for the UF open source assignment. I would really love to understand how the bug is fixed though so that I can make a meaningful pull request for experience (even if it is not accepted)

  11. story645 commented on Sep 29, 2022

    @story645
    Member

    @julia-harbord please look at a different issue; if you need help finding one, then please get in touch with us at one of the channels listed at https://matplotlib.org/devdocs/devel/contributing.html#get-connected

    We're a mostly volunteer team and every pull request requires folks to take their time to triage and review it; while we value your contribution and hope it helps your growth as a programmer, we feel it is a far better use of everyone's time to not duplicate work.

    @emmamastro and @FarukFS, do the boundaries of the assignment allow you two to collaborate on @FarukFS's PR as it is further along and already reviewed?

    ETA: I am also sorry @emmamastro that you didn't get the chance to put in your PR first, but our current policy is that issues can not be claimed except by putting in a PR so @FarukFS 's PR also has priority.

  12. linked a pull request that will close this issueCreated handler for PatchCollection #24028on Sep 29, 2022
  13. FarukFS commented on Sep 29, 2022

    @FarukFS

    @julia-harbord Sure. We can meet if you want, I can tell you about the changes that I made and why. I think that I am quite close to solving the issue, so it would probably be better to follow @story645 advice (I have some minor issues in my current PR, feel free to check it). As I said, we can meet and I can tell you a bit about the process, but don't expect too much, I'm also new :)

  14. anika3943 commented on Apr 20, 2025

    @anika3943

    Hi, interested in working on this issue!

  15. added this to the v3.11.0 milestone on Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions