Skip to content
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

plot label will not show up on legend if _ is used as first charcter in label #5200

Closed
PrometheusPi opened this issue Oct 6, 2015 · 4 comments

Comments

@PrometheusPi
Copy link

If the ASCII character 95 _ is used in as first character in a plot label, the label will not show up in the legend.

If only labels with _ in front are used, the following warning is given:

/opt/pkg/devel/python/3.4.3/lib/python3.4/site-packages/matplotlib-1.4.3-py3.4-linux-x86_64.egg/matplotlib/axes/_axes.py:475: UserWarning: No labelled objects found. Use label='...' kwarg on individual plots.
  warnings.warn("No labelled objects found. "

In order to test this bug, I tried a variety of matplotlib versions on Ubuntu and OS X.

  • 0.99.1.1
  • 1.1.0
  • 1.3.1
  • 1.4.3

All have the above behavior.

In order to reproduce the bug, I used the following minimal example:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

print(matplotlib.__version__)

x = np.linspace(-10, 10, 1024)
y = np.sin(x)

plt.plot(x,y, label="_abc")

plt.legend()
plt.show()

Using r"_abc" instead does not solve the problem.

@PrometheusPi
Copy link
Author

The reason for this behavior comes from this if clause.

It seems to be the intended behavior as documented:

Specific lines can be excluded from the automatic legend element selection by 
defining a label starting with an underscore. This is default for all artists, so 
calling legend() without any arguments and without setting the labels manually 
will result in no legend being drawn.

Sorry for the inconvenience. From my side, this issue can be closed.

@tacaswell
Copy link
Member

It is a surprising 'feature', but it has been that way for a long time and changing it would likely break a lot of code in the wild.

@jhollowed
Copy link

jhollowed commented Aug 31, 2021

this just sent me on a wild goose chase. All of my labels started with __, so I kept getting "No handles with labels found to put in legend."

This was very cryptic, and I could not solve this issue until finding this page. The error messaging on this should be improved.

@jklymak
Copy link
Member

jklymak commented Aug 31, 2021

The docs are relatively explicit about this https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html. Is there somewhere else you'd expect this information? If so, a PR would be most welcome.

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