From 72264bc2584dc27e2a33db32c3ab64b4d9124796 Mon Sep 17 00:00:00 2001 From: AALAM98mod100 Date: Sun, 30 Oct 2022 23:14:40 +0500 Subject: [PATCH 1/4] Added warning for underscores --- lib/matplotlib/legend.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index d0590824ad84..444671b8f9df 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1175,11 +1175,20 @@ def _get_legend_handles_labels(axs, legend_handler_map=None): """Return handles and labels for legend.""" handles = [] labels = [] + warning = False 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) + else: + warning = True + if warning: + log.warning("One or more labels starting with an underscore was detected. Note that " + "artists whose label start with an underscore are ignored " + "when legend() is called with no argument") + return handles, labels @@ -1254,9 +1263,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: From 0ccc865e16a1a024589445bd80ef9b8841700240 Mon Sep 17 00:00:00 2001 From: AALAM98mod100 Date: Sun, 30 Oct 2022 23:21:14 +0500 Subject: [PATCH 2/4] Added logger, flake8 formatting --- lib/matplotlib/legend.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 444671b8f9df..478501638b7b 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1173,6 +1173,7 @@ 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 = [] warning = False @@ -1185,10 +1186,10 @@ def _get_legend_handles_labels(axs, legend_handler_map=None): else: warning = True if warning: - log.warning("One or more labels starting with an underscore was detected. Note that " - "artists whose label start with an underscore are ignored " - "when legend() is called with no argument") - + log.warning("One or more 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 From 5e0a8483da3494c2ab9092c35f37ed715598ddb9 Mon Sep 17 00:00:00 2001 From: AALAM98mod100 Date: Mon, 31 Oct 2022 20:09:15 +0500 Subject: [PATCH 3/4] Added flag to detect distinction between empty handles vs all labels starting with underscores --- lib/matplotlib/legend.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 478501638b7b..fb5cf46b342d 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1176,17 +1176,22 @@ def _get_legend_handles_labels(axs, legend_handler_map=None): log = logging.getLogger(__name__) handles = [] labels = [] - warning = False + all_underscored = None for handle in _get_legend_handles(axs, legend_handler_map): label = handle.get_label() if label: if not label.startswith('_'): + # it has found atleast one _ char handles.append(handle) labels.append(label) + all_underscored = False else: - warning = True - if warning: - log.warning("One or more labels starting with an underscore were " + all_underscored = True if all_underscored is not False \ + else False + + # if it has found a label and not one of them starts with _ + if all_underscored is not None and 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") From 5f26feeba2b03c983b9efc3b17166cc6860e9fe3 Mon Sep 17 00:00:00 2001 From: AALAM98mod100 Date: Mon, 31 Oct 2022 20:21:20 +0500 Subject: [PATCH 4/4] Cleaned up code/condition --- lib/matplotlib/legend.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index fb5cf46b342d..ce0eaa056815 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1181,7 +1181,6 @@ def _get_legend_handles_labels(axs, legend_handler_map=None): label = handle.get_label() if label: if not label.startswith('_'): - # it has found atleast one _ char handles.append(handle) labels.append(label) all_underscored = False @@ -1189,8 +1188,7 @@ def _get_legend_handles_labels(axs, legend_handler_map=None): all_underscored = True if all_underscored is not False \ else False - # if it has found a label and not one of them starts with _ - if all_underscored is not None and all_underscored is True: + 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 "