Skip to content

Bad boxplot legend entries #20512

Closed

Description

@RSKothari

Describe the issue

Legend for entries in boxplot are displayed as thin lines as opposed to patches

When we have multiple box plots in the same axis, legend shows thin lines as opposed to boxes and it is difficult to tell entries apart. This issue has been documented at this link with a reproducible example.

Proposed fix

I don't have a solution :(

Link: https://stackoverflow.com/questions/68124486/improve-matplotlib-legend-for-boxplots-by-showing-patches-instead-of-lines

Activity

  1. story645 commented on Jun 25, 2021

    @story645
    Member

    Do you think the legend should use boxes as illustrated in the answer to that question? fGWNP.png

  2. RSKothari commented on Jun 25, 2021

    @RSKothari
    Author

    @story645 Yes, I think that should be the default behavior of legend when used on boxplot.

  3. anntzer commented on Jun 25, 2021

    @anntzer
    Contributor

    I think this is mostly a matter of setting label="_nolegend_" on all artists generated by boxplot except the box itself (the black lines in the SO example come from the fact that we currently try to generate legend entries for the whiskers, caps, and outliers).
    Even better would be to have a custom handler for boxplots, but that seems to be much more work.

  4. saranti commented on Jun 14, 2023

    @saranti
    Contributor

    Ever since v3.5, the first entry in the legend is shown as a rectangular patch with the rest shown as lines:

    box

    It's the same regardless of the number of entries.

  5. story645 commented on Jun 14, 2023

    @story645
    Member

    code to reproduce:

    fig, axs = plt.subplots()
    
    A = 5*np.random.rand(100, 1)
    B = 10*np.random.rand(100, 1) -5
    C = 7*np.random.rand(100, 1) - 5
    bp0 = axs.boxplot(A, positions=[0], patch_artist=True)
    bp1 = axs.boxplot(B, positions=[1], patch_artist=True)
    bp2 = axs.boxplot(C, positions=[2], patch_artist=True)
    axs.legend(['A', 'B', 'C'])

    ET2: I haven't done a bisect, but #20515 went in around 3.5 and refactored the boxplot code.

  6. added this to the v3.9.0 milestone on Feb 1, 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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions