Skip to content

Create distinction between warnings for no-legend handles. #24311

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

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/matplotlib/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1173,13 +1173,26 @@ def _get_legend_handles(axs, legend_handler_map=None):

def _get_legend_handles_labels(axs, legend_handler_map=None):
"""Return handles and labels for legend."""
log = logging.getLogger(__name__)
handles = []
labels = []
all_underscored = None
for handle in _get_legend_handles(axs, legend_handler_map):
label = handle.get_label()
if label and not label.startswith('_'):
handles.append(handle)
labels.append(label)
if label:
if not label.startswith('_'):
handles.append(handle)
labels.append(label)
all_underscored = False
else:
all_underscored = True if all_underscored is not False \
else False

if all_underscored is True:
log.warning("All labels starting with an underscore were "
"detected. Note that artists whose label start with an "
"underscore are ignored when legend() is called with "
"no argument")
return handles, labels


Expand Down Expand Up @@ -1254,9 +1267,7 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs):
handles, labels = _get_legend_handles_labels(axs, handlers)
if not handles:
log.warning(
"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.")
"No handles with labels found to put in legend.")

# One argument. User defined labels - automatic handle detection.
elif len(args) == 1:
Expand Down