Skip to content

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

Closed

Description

@PrometheusPi

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.

Activity

  1. PrometheusPi commented on Oct 6, 2015

    @PrometheusPi
    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.

  2. tacaswell commented on Oct 6, 2015

    @tacaswell
    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.

  3. jhollowed commented on Aug 31, 2021

    @jhollowed

    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.

  4. jklymak commented on Aug 31, 2021

    @jklymak
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions