Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gpelouze opened this issue Dec 20, 2017 · 5 comments
Closed
Assignees
Milestone

Comments

@gpelouze
Copy link

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.
@tacaswell tacaswell added this to the v2.1.2 milestone Dec 20, 2017
@tacaswell
Copy link
Member

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

@jklymak jklymak self-assigned this Dec 20, 2017
@jklymak
Copy link
Member

jklymak commented Dec 20, 2017

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.

@jklymak
Copy link
Member

jklymak commented Dec 21, 2017

Closed #10064. Thanks for the bug report!

@jklymak jklymak closed this as completed Dec 21, 2017
@HappyMeeple
Copy link

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

However, it should be fixed, right?

@jklymak
Copy link
Member

jklymak commented Nov 29, 2021

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants