From 46ae61c47123f3f846b8952a9ca69cb023b73c7c Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 14 May 2021 20:50:08 -0400 Subject: [PATCH] Backport PR #20225: FIX: correctly handle ax.legend(..., legendcolor='none') --- lib/matplotlib/legend.py | 3 +++ lib/matplotlib/tests/test_legend.py | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index ff557816a865..05453584f629 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -534,6 +534,9 @@ def __init__(self, parent, handles, labels, break except AttributeError: pass + elif isinstance(labelcolor, str) and labelcolor == 'none': + for text in self.texts: + text.set_color(labelcolor) elif np.iterable(labelcolor): for text, color in zip(self.texts, itertools.cycle( diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index cebf26ea066e..12356ae15667 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -552,16 +552,17 @@ def test_legend_title_fontsize(): assert leg.get_title().get_fontsize() == 22 -def test_legend_labelcolor_single(): +@pytest.mark.parametrize('color', ('red', 'none', (.5, .5, .5))) +def test_legend_labelcolor_single(color): # test labelcolor for a single color fig, ax = plt.subplots() ax.plot(np.arange(10), np.arange(10)*1, label='#1') ax.plot(np.arange(10), np.arange(10)*2, label='#2') ax.plot(np.arange(10), np.arange(10)*3, label='#3') - leg = ax.legend(labelcolor='red') + leg = ax.legend(labelcolor=color) for text in leg.get_texts(): - assert mpl.colors.same_color(text.get_color(), 'red') + assert mpl.colors.same_color(text.get_color(), color) def test_legend_labelcolor_list():