Description
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.