Skip to content

Change the size of legend markers... #12417

Open

Description

@jklymak

Closed a PR that did this, which was probably over engineered. But the main idea is probably still useful.

#11127 is a bit different, it would create many legend entries for a single scatterplot. The idea here was to unify the size of different scatter plot's handles. Many options are shown in the stackoverflow question "setting a fixed size for points in legend". The easiest is just:

legend = plt.legend()
for legobj in legend.legendHandles:
    legobj.set_sizes([64])

My faviourite, which I just added to that thread as well would be to use the update function of the Handler.

def update(handle, orig):
    handle.update_from(orig)
    handle.set_sizes([64])

plt.legend(handler_map={PathCollection : HandlerPathCollection(update_func=update)})

Originally posted by @ImportanceOfBeingErnest in #4257 (comment)

Activity

  1. h-vetinari commented on Oct 18, 2018

    @h-vetinari

    Saved my day. This really should work out of the box. Why do the legend markers change size at all...? I get the point where the only legend info is the size of the marker, but often it's colors/categories etc. that should be in the legend first and foremost.

  2. ImportanceOfBeingErnest commented on Oct 18, 2018

    @ImportanceOfBeingErnest
    Member

    Why do the legend markers change size at all...?

    Usually you want a legend to show the marker in the same size as it appears in the plot. For a scatter with different sizes, there is no general way to determine which of the used sizes would be the best to show in the legend - this requires some user input.

    But of course feel free to make a suggestion here. Given two scatter plots

    sc1 = ax.scatter(..., s=[5,10,15])
    sc2 = ax.scatter(..., s=[32,64,128])
    

    which "automatic" size would you expect to see in a legend?

  3. h-vetinari commented on Oct 18, 2018

    @h-vetinari

    which "automatic" size would you expect to see in a legend?

    Just the normal fontsize of the legend? Or, maybe more accurately, a constant size so that the markers in the legend do not overlap -- something visually pleasing w.r.t. to the already fixed linespacing size (IOW, maybe slightly larger than the fontsize, but smaller than fontsize+linespacing).

  4. ImportanceOfBeingErnest commented on Oct 18, 2018

    @ImportanceOfBeingErnest
    Member

    Actually, I don't know why #4247 wasn't pursued further. It looks much more straight forward than #4257.
    I think @h-vetinari 's suggestion could be easily incoorporated as an uniform_size="auto" parameter (although it might not be possible to easily update the legend once fontsize or linespacing change after legend creation).

  5. bf commented on May 7, 2020

    @bf

    It'd be great if this is further pursued by someone who is able to implement it. It is one of the most important things that's missing in my day-to-day pyplot use.

  6. github-actions commented on Jun 7, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  7. added
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jul 10, 2023
  8. h-vetinari commented on Jul 10, 2023

    @h-vetinari

    Pity that this got closed, I missed the ping a year ago... 😑

  9. rcomer commented on Jul 10, 2023

    @rcomer
    Member

    @h-vetinari we are happy to re-open these bot-closed issues if someone is still interested in them.

  10. reopened this on Jul 10, 2023
  11. removed
    status: inactiveMarked by the “Stale” Github Action
    status: closed as inactiveIssues closed by the "Stale" Github Action. Please comment on any you think should still be open.
    on Jul 10, 2023
  12. github-actions commented on Oct 2, 2024

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  13. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    keepItems to be ignored by the “Stale” Github Actiontopic: legend

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions