-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[ENH]: Make "No artists with labels found to put in legend" a warning #27145
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
Comments
This is related to #23422 though this one is a little more ambiguous if it should actually be a warning or not as the user can suppress the warning by either adding a label or removing the legend call. |
Thanks for the link :) My issue is definitely an example of @mwaskom's point that "you can convert a warning to an error and then drop into a debugger to figure out what's happening" Also, I'm not sure I understand how its ambiguous? Your statement:
sounds like the standard, "the issue is avoidable and the client application should be modified to eliminate the warning." I can't think of a situation where a user would legitimately intend to call |
I agree, but I don't remember the original rationale for a logging call versus warning, and the rule itself is a bit ambiguous. |
TL;DR: logging was added to this module at a time when you were making the package-wide effort of replacing the old Doing a bit of
That was the extent of discussion I could find about logging vs warning. Another commit later amended the message (#20956) but didn't touch on logging vs warning. A pickaxe search of the git log for "logging" around the same time finds another commit (844877c) & PR (#9313) that address a discussion (#9302) about replacing matplotlib's old |
Thanks for the archeology 🚀! I conclude that the current state has evolved organically and there has probably not been a conscious decision between logging and warnings for this specific case. Either way,
plus
is a clear indication for me that this should be a |
@Jacob-Stevens-Haas Did you have bandwidth to put in a PR to change this? |
I think so! Got the dev environment/install up and working, and all tests (at least all relevant tests) pass, so I can start a PR. |
Am I supposed to add a file to matplotlib/doc/users/next_whats_new for this kind of change? Also, should I keep the default module logger definition |
…27172) * ENH: Change logging to warning when creating a legend with no labels Fixes #27145 Also add test_legend_nolabels_warning() * TST: Add mock labels to mock data. * DOC: Note warning in legend() documentation * DOC: Correct examples to no longer raise UserWarning With modified explanation where appropriate * TST: Add test for drawing empty legend() * TST: Test legend for specific warning when no labels present Also: DOC: Correct Axes docstring when no labels in legend. * TST: Filter no labels warning by message. Also assert that a legend is at least created
Uh oh!
There was an error while loading. Please reload this page.
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:
(no error, plot appears)
Proposed solution
Make this a true
UserWarning
. Currently, it's alogging.LogRecord
of level=logging.Warning
.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 callinglogging.setLoggerClass()
) but does not apply a similar philosophy to capturing warnings in STDERR.The text was updated successfully, but these errors were encountered: