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..29729bbb15a1 100644 --- a/lib/matplotlib/tests/test_container.py +++ b/lib/matplotlib/tests/test_container.py @@ -28,3 +28,19 @@ 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") + + handles, labels = ax.get_legend_handles_labels() + + assert "foo" not in labels + + assert len(handles) == 1