Skip to content

Only one legend entry is rendered for items with the same label and color #10056

Closed

Description

@gpelouze

Only one legend entry is rendered for items with the same label and color

In Matplotlib 2.1.1, items that have the same label and color result in a single legend entry. This happens even if some other properties of the items are different (eg. marker or linewidth).

This behaviour is surprising because the style of the first item is used for the handle, regardless of the style of the following items.

I would expect either the behaviour from Matplotlib 2.1.0, or the labels being merged only if the label and all style properties are equal.

Code for reproduction

import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
plt.plot([0, 1], [0, 1], label='foo', color='k', marker='o', linewidth=1)
plt.plot([0, 1], [1, 2], label='foo', color='k', marker='.', linewidth=2)
plt.plot([0, 1], [2, 3], label='bar', color='k', marker='s', linewidth=3)
plt.legend()
plt.savefig('foobar.png')

Actual outcome (Matplotlib 2.1.1)

foobar_211

Expected outcome (Matplotlib 2.1.0)

foobar_210

Matplotlib version

  • Operating system: Arch Linux
  • Matplotlib version: 2.1.1
  • Matplotlib backend: agg
  • Python version: 3.6.3
  • Matplotlib installed from Arch Linux repository community/python-matplotlib.

Activity

  1. added this to the v2.1.2 milestone on Dec 20, 2017
  2. tacaswell commented on Dec 20, 2017

    @tacaswell
    Member

    attn @jklymak Looks like more issues with the de-duplication logic.

  3. self-assigned this
    on Dec 20, 2017
  4. jklymak commented on Dec 20, 2017

    @jklymak
    Member

    OK, I see what has happened finally. Sorry to be so slow.

    The de-duplication logic was only in Figure.legend not in Axes.legend (or pyplot.legend()).

    The old Figure.legend de-duplication had this bug and the bug reported in #10030.

    What I did in #9324 was make the parsing logic the same. So the de-duplciation logic ended up in Axes.legend and that means people who weren't triggering the bug now are triggering the bug.

    I propose we get rid of the de-duplciation logic for both versions of legend. That is a breaking API change for Figure.legend() but I think it is a pretty clear edge case. I think its better to do that than to keep the de-duplication logic for Axes.legend() which was also a breaking change that we (I) didn't note at the time.

  5. jklymak commented on Dec 21, 2017

    @jklymak
    Member

    Closed #10064. Thanks for the bug report!

  6. HappyMeeple commented on Nov 28, 2021

    @HappyMeeple

    I experience the same bug, or so I think with Matplotlib 3.4.3.

    However, it should be fixed, right?

  7. jklymak commented on Nov 29, 2021

    @jklymak
    Member

    @HappyMeeple the original report example works fine for me, so if you have a similar error, please open a new issue with a self-contained minimal example. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions