Skip to content

Commit 6cc7cda

Browse files
authored
Merge pull request #24143 from anntzer/cr
Add QuadContourSet.remove.
2 parents dad3ae3 + 67ed913 commit 6cc7cda

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

lib/matplotlib/contour.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
import matplotlib as mpl
1212
from matplotlib import _api, _docstring
1313
from matplotlib.backend_bases import MouseButton
14+
from matplotlib.text import Text
1415
import matplotlib.path as mpath
1516
import matplotlib.ticker as ticker
1617
import matplotlib.cm as cm
1718
import matplotlib.colors as mcolors
1819
import matplotlib.collections as mcoll
1920
import matplotlib.font_manager as font_manager
20-
import matplotlib.text as text
2121
import matplotlib.cbook as cbook
2222
import matplotlib.patches as mpatches
2323
import matplotlib.transforms as mtransforms
@@ -31,7 +31,7 @@
3131
# per level.
3232

3333

34-
class ClabelText(text.Text):
34+
class ClabelText(Text):
3535
"""
3636
Unlike the ordinary text, the get_rotation returns an updated
3737
angle in the pixel coordinate assuming that the input rotation is
@@ -253,11 +253,10 @@ def _get_nth_label_width(self, nth):
253253
fig = self.axes.figure
254254
renderer = fig._get_renderer()
255255
return (
256-
text.Text(0, 0,
257-
self.get_text(self.labelLevelList[nth], self.labelFmt),
258-
figure=fig,
259-
size=self.labelFontSizeList[nth],
260-
fontproperties=self.labelFontProps)
256+
Text(0, 0, self.get_text(self.labelLevelList[nth], self.labelFmt),
257+
figure=fig,
258+
size=self.labelFontSizeList[nth],
259+
fontproperties=self.labelFontProps)
261260
.get_window_extent(renderer).width)
262261

263262
@_api.deprecated("3.5")
@@ -267,8 +266,8 @@ def get_label_width(self, lev, fmt, fsize):
267266
lev = self.get_text(lev, fmt)
268267
fig = self.axes.figure
269268
renderer = fig._get_renderer()
270-
width = (text.Text(0, 0, lev, figure=fig,
271-
size=fsize, fontproperties=self.labelFontProps)
269+
width = (Text(0, 0, lev, figure=fig,
270+
size=fsize, fontproperties=self.labelFontProps)
272271
.get_window_extent(renderer).width)
273272
width *= 72 / fig.dpi
274273
return width
@@ -419,10 +418,9 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5):
419418

420419
def _get_label_text(self, x, y, rotation):
421420
dx, dy = self.axes.transData.inverted().transform((x, y))
422-
t = text.Text(dx, dy, rotation=rotation,
423-
horizontalalignment='center',
424-
verticalalignment='center', zorder=self._clabel_zorder)
425-
return t
421+
return Text(dx, dy, rotation=rotation,
422+
horizontalalignment='center',
423+
verticalalignment='center', zorder=self._clabel_zorder)
426424

427425
def _get_label_clabeltext(self, x, y, rotation):
428426
# x, y, rotation is given in pixel coordinate. Convert them to
@@ -585,6 +583,10 @@ def labels(self, inline, inline_spacing):
585583
if inline:
586584
paths[:] = additions
587585

586+
def remove(self):
587+
for text in self.labelTexts:
588+
text.remove()
589+
588590

589591
def _is_closed_polygon(X):
590592
"""
@@ -1389,6 +1391,11 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True):
13891391

13901392
return (conmin, segmin, imin, xmin, ymin, d2min)
13911393

1394+
def remove(self):
1395+
super().remove()
1396+
for coll in self.collections:
1397+
coll.remove()
1398+
13921399

13931400
@_docstring.dedent_interpd
13941401
class QuadContourSet(ContourSet):

lib/matplotlib/tests/test_contour.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,13 @@ def test_negative_linestyles(style):
682682
ax4.clabel(CS4, fontsize=9, inline=True)
683683
ax4.set_title(f'Single color - negative contours {style}')
684684
assert CS4.negative_linestyles == style
685+
686+
687+
def test_contour_remove():
688+
ax = plt.figure().add_subplot()
689+
orig_children = ax.get_children()
690+
cs = ax.contour(np.arange(16).reshape((4, 4)))
691+
cs.clabel()
692+
assert ax.get_children() != orig_children
693+
cs.remove()
694+
assert ax.get_children() == orig_children

0 commit comments

Comments
 (0)