From 3e9a88ec5410de3c916593ff607d8275dbd46726 Mon Sep 17 00:00:00 2001 From: Michael Higgins <55243596+Higgs32584@users.noreply.github.com> Date: Sun, 5 Mar 2023 22:57:56 +0000 Subject: [PATCH 1/2] fixed issue#25274 --- lib/matplotlib/container.py | 1 + lib/matplotlib/tests/test_container.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/matplotlib/container.py b/lib/matplotlib/container.py index 62fe9a35e666..29ff5bbc1067 100644 --- a/lib/matplotlib/container.py +++ b/lib/matplotlib/container.py @@ -26,6 +26,7 @@ def remove(self): self, scalarp=lambda x: isinstance(x, Artist)): if c is not None: c.remove() + self.set_label("") if self._remove_method: self._remove_method(self) diff --git a/lib/matplotlib/tests/test_container.py b/lib/matplotlib/tests/test_container.py index 8e894d9e9084..e9d305457df6 100644 --- a/lib/matplotlib/tests/test_container.py +++ b/lib/matplotlib/tests/test_container.py @@ -28,3 +28,20 @@ def test_errorbar_remove(): eb = ax.errorbar([1], [1], fmt='none') eb.remove() + +def test_removal_from_legend(): + ax = plt.gca() + + obj1 = ax.errorbar([1], [1], [1], marker="o", label="foo") + + obj1.remove() + + ax.errorbar([1.1], [1.1], [1], marker="o", label="bar") + + plt.legend() + + handles, labels = ax.get_legend_handles_labels() + + assert "foo" not in labels + + assert len(handles) == 1 From f56a13f41baf345035bd4494f5b272edc6496493 Mon Sep 17 00:00:00 2001 From: Michael Higgins <55243596+Higgs32584@users.noreply.github.com> Date: Mon, 6 Mar 2023 13:56:28 +0000 Subject: [PATCH 2/2] fixed flake8 issue --- lib/matplotlib/tests/test_container.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/tests/test_container.py b/lib/matplotlib/tests/test_container.py index e9d305457df6..29729bbb15a1 100644 --- a/lib/matplotlib/tests/test_container.py +++ b/lib/matplotlib/tests/test_container.py @@ -29,6 +29,7 @@ def test_errorbar_remove(): eb = ax.errorbar([1], [1], fmt='none') eb.remove() + def test_removal_from_legend(): ax = plt.gca() @@ -38,8 +39,6 @@ def test_removal_from_legend(): ax.errorbar([1.1], [1.1], [1], marker="o", label="bar") - plt.legend() - handles, labels = ax.get_legend_handles_labels() assert "foo" not in labels