From fe9e9e9e3afb9327d79dba419acfd9967c445abc Mon Sep 17 00:00:00 2001 From: Klayto-235 Date: Tue, 13 Dec 2022 13:03:43 +0100 Subject: [PATCH 1/4] Fixed behaviour of matplotlib.legend.Legend with respect to the handles and labels parameters to adhere to its documentaion. --- lib/matplotlib/legend.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index d0590824ad84..cd1c26693b91 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -418,17 +418,6 @@ def val_or_rc(val, rc_name): self.borderaxespad = val_or_rc(borderaxespad, 'legend.borderaxespad') self.columnspacing = val_or_rc(columnspacing, 'legend.columnspacing') self.shadow = val_or_rc(shadow, 'legend.shadow') - # trim handles and labels if illegal label... - _lab, _hand = [], [] - for label, handle in zip(labels, handles): - if isinstance(label, str) and label.startswith('_'): - _api.warn_external(f"The label {label!r} of {handle!r} starts " - "with '_'. It is thus excluded from the " - "legend.") - else: - _lab.append(label) - _hand.append(handle) - labels, handles = _lab, _hand handles = list(handles) if len(handles) < 2: From e54aeed4037113d3972361bff86931891d0821cd Mon Sep 17 00:00:00 2001 From: Klayto-235 Date: Tue, 13 Dec 2022 13:56:22 +0100 Subject: [PATCH 2/4] Adapted tests for the previous fix. --- lib/matplotlib/legend.py | 7 ++----- lib/matplotlib/tests/test_legend.py | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/legend.py b/lib/matplotlib/legend.py index cd1c26693b91..c95b59a40197 100644 --- a/lib/matplotlib/legend.py +++ b/lib/matplotlib/legend.py @@ -22,7 +22,6 @@ """ import itertools -import logging import time import numpy as np @@ -1217,8 +1216,6 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): *kwargs* with keywords handles and labels removed. """ - log = logging.getLogger(__name__) - handlers = kwargs.get('handler_map') extra_args = () @@ -1242,8 +1239,8 @@ def _parse_legend_args(axs, *args, handles=None, labels=None, **kwargs): elif len(args) == 0: handles, labels = _get_legend_handles_labels(axs, handlers) if not handles: - log.warning( - "No artists with labels found to put in legend. Note that " + _api.warn_external( + "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.") diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 6660b91ecdd9..8e4ff3172c02 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -80,20 +80,32 @@ def test_various_labels(): ax.legend(numpoints=1, loc='best') -def test_legend_label_with_leading_underscore(): +def test_legend_label_with_leading_underscore1(): """ Test that artists with labels starting with an underscore are not added to - the legend, and that a warning is issued if one tries to add them - explicitly. + the legend when the legend method is called with no arguments, and that a + warning is issued if one tries to add them explicitly. """ fig, ax = plt.subplots() - line, = ax.plot([0, 1], label='_foo') + ax.plot([0, 1], label='_foo') with pytest.warns(UserWarning, - match=r"starts with '_'.*excluded from the legend."): - legend = ax.legend(handles=[line]) + match=r"artists whose label start with an underscore " + "are ignored"): + legend = ax.legend() assert len(legend.legendHandles) == 0 +def test_legend_label_with_leading_underscore2(): + """ + Test that artists with labels starting with an underscore are added to the + legend when the legend method is called with the handles argument. + """ + fig, ax = plt.subplots() + lines = ax.plot([0, 1], label='_foo') + legend = ax.legend(handles=lines) + assert len(legend.legendHandles) == 1 + + @image_comparison(['legend_labels_first.png'], remove_text=True) def test_labels_first(): # test labels to left of markers From 73da1bc110fc8928efd9be86af9114cb3f39cfad Mon Sep 17 00:00:00 2001 From: Klayto-235 Date: Tue, 13 Dec 2022 16:25:33 +0100 Subject: [PATCH 3/4] Fixed newly failing tests --- lib/matplotlib/tests/test_legend.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 8e4ff3172c02..5f0bcb848333 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -524,9 +524,10 @@ def test_text_nohandler_warning(): """Test that Text artists with labels raise a warning""" fig, ax = plt.subplots() ax.text(x=0, y=0, s="text", label="label") - with pytest.warns(UserWarning) as record: + with pytest.warns(UserWarning, + match="Legend does not support handles for .+ " + "instances"): ax.legend() - assert len(record) == 1 # this should _not_ warn: f, ax = plt.subplots() @@ -558,7 +559,7 @@ def test_legend_title_empty(): # it comes back as an empty string, and that it is not # visible: fig, ax = plt.subplots() - ax.plot(range(10)) + ax.plot(range(10), label="foo") leg = ax.legend() assert leg.get_title().get_text() == "" assert not leg.get_title().get_visible() @@ -591,7 +592,7 @@ def test_window_extent_cached_renderer(): def test_legend_title_fontprop_fontsize(): # test the title_fontsize kwarg - plt.plot(range(10)) + plt.plot(range(10), label="foo") with pytest.raises(ValueError): plt.legend(title='Aardvark', title_fontsize=22, title_fontproperties={'family': 'serif', 'size': 22}) @@ -602,27 +603,27 @@ def test_legend_title_fontprop_fontsize(): fig, axes = plt.subplots(2, 3, figsize=(10, 6)) axes = axes.flat - axes[0].plot(range(10)) + axes[0].plot(range(10), label="foo") leg0 = axes[0].legend(title='Aardvark', title_fontsize=22) assert leg0.get_title().get_fontsize() == 22 - axes[1].plot(range(10)) + axes[1].plot(range(10), label="foo") leg1 = axes[1].legend(title='Aardvark', title_fontproperties={'family': 'serif', 'size': 22}) assert leg1.get_title().get_fontsize() == 22 - axes[2].plot(range(10)) + axes[2].plot(range(10), label="foo") mpl.rcParams['legend.title_fontsize'] = None leg2 = axes[2].legend(title='Aardvark', title_fontproperties={'family': 'serif'}) assert leg2.get_title().get_fontsize() == mpl.rcParams['font.size'] - axes[3].plot(range(10)) + axes[3].plot(range(10), label="foo") leg3 = axes[3].legend(title='Aardvark') assert leg3.get_title().get_fontsize() == mpl.rcParams['font.size'] - axes[4].plot(range(10)) + axes[4].plot(range(10), label="foo") mpl.rcParams['legend.title_fontsize'] = 20 leg4 = axes[4].legend(title='Aardvark', title_fontproperties={'family': 'serif'}) assert leg4.get_title().get_fontsize() == 20 - axes[5].plot(range(10)) + axes[5].plot(range(10), label="foo") leg5 = axes[5].legend(title='Aardvark') assert leg5.get_title().get_fontsize() == 20 @@ -907,7 +908,7 @@ def test_legend_labelcolor_rcparam_markerfacecolor_short(): def test_get_set_draggable(): - legend = plt.legend() + legend = plt.legend("foo") assert not legend.get_draggable() legend.set_draggable(True) assert legend.get_draggable() From abad6cc8d8c21156149c0a693d037b8e74e28084 Mon Sep 17 00:00:00 2001 From: Klayto-235 Date: Tue, 13 Dec 2022 16:52:39 +0100 Subject: [PATCH 4/4] Implemented more complete test --- lib/matplotlib/tests/test_legend.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/tests/test_legend.py b/lib/matplotlib/tests/test_legend.py index 5f0bcb848333..024441623e5f 100644 --- a/lib/matplotlib/tests/test_legend.py +++ b/lib/matplotlib/tests/test_legend.py @@ -98,12 +98,20 @@ def test_legend_label_with_leading_underscore1(): def test_legend_label_with_leading_underscore2(): """ Test that artists with labels starting with an underscore are added to the - legend when the legend method is called with the handles argument. + legend when the legend method is called with arguments. """ fig, ax = plt.subplots() lines = ax.plot([0, 1], label='_foo') legend = ax.legend(handles=lines) assert len(legend.legendHandles) == 1 + legend = ax.legend(handles=lines, labels=["_foo"]) + assert len(legend.legendHandles) == 1 + legend = ax.legend(labels=["_foo"]) + assert len(legend.legendHandles) == 1 + legend = ax.legend(["_foo"]) + assert len(legend.legendHandles) == 1 + legend = ax.legend(lines, ["_foo"]) + assert len(legend.legendHandles) == 1 @image_comparison(['legend_labels_first.png'], remove_text=True) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy