Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ff2eab9

Browse files
committed
FIX: Address review feedback - simpler fix and cleaner test
1 parent ada7b70 commit ff2eab9

2 files changed

Lines changed: 13 additions & 18 deletions

File tree

lib/matplotlib/contour.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,12 @@ def labels(self, inline, inline_spacing):
513513
self._paths[icon] = Path.make_compound_path(*additions)
514514

515515
def remove(self):
516-
axes = self.axes
517516
super().remove()
518517
for text in list(self.labelTexts):
519-
if axes is not None and text in axes.texts:
518+
try:
520519
text.remove()
520+
except ValueError:
521+
pass
521522
self.labelTexts.clear()
522523

523524

lib/matplotlib/tests/test_contour.py

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,16 @@ def test_contour_remove():
804804
assert ax.get_children() == orig_children
805805

806806

807+
def test_contour_remove_with_labels():
808+
ax = plt.figure().add_subplot()
809+
cs = ax.contour(np.arange(16).reshape((4, 4)))
810+
labels = cs.clabel()
811+
for label in labels:
812+
label.remove()
813+
cs.clabel()
814+
cs.remove()
815+
816+
807817
def test_contour_no_args():
808818
fig, ax = plt.subplots()
809819
data = [[0, 1], [1, 0]]
@@ -877,19 +887,3 @@ def test_contour_singular_color():
877887
with pytest.raises(TypeError):
878888
plt.figure().add_subplot().contour([[0, 1], [2, 3]], color="r")
879889

880-
881-
882-
def test_contour_remove_after_label_removed():
883-
# Test that CS.remove() works even if labels were manually removed first
884-
# Regression test for https://github.com/matplotlib/matplotlib/issues/31404
885-
fig, ax = plt.subplots()
886-
x = np.linspace(-3.0, 3.0, 20)
887-
y = np.linspace(-2.0, 2.0, 20)
888-
X, Y = np.meshgrid(x, y)
889-
Z = np.exp(-X**2 - Y**2)
890-
CS = ax.contour(X, Y, Z)
891-
labels = ax.clabel(CS, colors='k')
892-
for label in labels:
893-
label.remove()
894-
ax.clabel(CS, colors='r')
895-
CS.remove() # Should not raise ValueError

0 commit comments

Comments
 (0)