From 7805ac8051a16f1c4618682a5dad54640e64f87e Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Sat, 27 Aug 2022 16:00:35 -0700 Subject: [PATCH] FIX: legend handler warning too liberal --- lib/matplotlib/legend.py | 2 +- lib/matplotlib/tests/test_legend.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index 59fbb21a4de4..9e5b903d59ab 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -1140,7 +1140,7 @@ def _get_legend_handles(axs, legend_handler_map=None): label = handle.get_label() if label != '_nolegend_' and has_handler(handler_map, handle): yield handle - elif (label not in ['_nolegend_', ''] and + elif (label and not label.startswith('_') and not has_handler(handler_map, handle)): _api.warn_external( "Legend does not support handles for {0} " diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index dff45126bf56..16847e0be6e2 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -1,6 +1,7 @@ import collections import platform from unittest import mock +import warnings import numpy as np import pytest @@ -515,6 +516,13 @@ def test_text_nohandler_warning(): ax.legend() assert len(record) == 1 + # this should _not_ warn: + f, ax = plt.subplots() + ax.pcolormesh(np.random.uniform(0, 1, (10, 10))) + with warnings.catch_warnings(): + warnings.simplefilter("error") + ax.get_legend_handles_labels() + def test_empty_bar_chart_with_legend(): """Test legend when bar chart is empty with a label."""