Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[ENH]: Make "No artists with labels found to put in legend" a warning #27145

Copy link
Copy link
Closed
@Jacob-Stevens-Haas

Description

@Jacob-Stevens-Haas
Issue body actions

Problem

When I receive this issue, it's hard to debug and find the specific call to ax.legend() if I can't raise Warnings to Exceptions. I sometimes nest plotting functions in order to draw the same plot different ways or arrange plots in a composite figure, depending upon the presentation.

MWE and Full message:

from matplotlib import pyplot as plt
import warnings

warnings.simplefilter("error", category=Warning)
plt.plot([1,2,3])
plt.legend()

(no error, plot appears)

No artists with labels found to put in legend. Note that artists whose label start with an underscore are ignored when legend() is called with no argument.

Proposed solution

Make this a true UserWarning. Currently, it's a logging.LogRecord of level=logging.Warning.

log = logging.getLogger(__name__)
...
log.warning(<this message>)

Additional pro: In the future, it would be easier to find and work on this warning if it is, in fact, a true Warning.

Con: It won't be handled by the logging infrastructure. But legend.py uses the default module logger, and the only LogRecord that it emits is this one. The only situation I could think of where this might matter is if a user has some override of global logging setup (by calling logging.setLoggerClass()) but does not apply a similar philosophy to capturing warnings in STDERR.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.