Skip to content

[Bug]: Crash when removing contour set after removing contour labels #31404

@williamlus

Description

@williamlus

Bug summary

Create a contour plot CS, add contour labels based on CS, remove the contour labels, then remove CS.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2

fig, ax = plt.subplots()
ax.contourf(X, Y, Z)
CS = ax.contour(X, Y, Z, visible=False)
labels = ax.clabel(CS, colors='k')
for label in labels: label.remove()
labels = ax.clabel(CS, colors='r')
CS.remove()

Actual outcome


ValueError Traceback (most recent call last)
Cell In[1], line 18
16 for label in labels: label.remove()
17 labels = ax.clabel(CS, colors='r')
---> 18 CS.remove()

File ~/dataviz-render-update-testing/matplotlib/lib/matplotlib/contour.py:518, in ContourLabeler.remove(self)
516 super().remove()
517 for text in self.labelTexts:
--> 518 text.remove()

File ~/dataviz-render-update-testing/matplotlib/lib/matplotlib/artist.py:254, in Artist.remove(self)
249 # There is no method to set the callback. Instead, the parent should
250 # set the _remove_method attribute directly. This would be a
251 # protected attribute if Python supported that sort of thing. The
252 # callback has one parameter, which is the child to be removed.
253 if self._remove_method is not None:
--> 254 self._remove_method(self)
255 # clear stale callback
256 self.stale_callback = None

ValueError: list.remove(x): x not in list

Expected outcome

Image

Additional information

No response

Operating system

No response

Matplotlib Version

0.2.0.dev53961+unknown.g607d2c380.g9e38f5437

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions