diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 2f8c934e70b9..f1a502bcf824 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -11,13 +11,13 @@ import matplotlib as mpl from matplotlib import _api, _docstring from matplotlib.backend_bases import MouseButton +from matplotlib.text import Text import matplotlib.path as mpath import matplotlib.ticker as ticker import matplotlib.cm as cm import matplotlib.colors as mcolors import matplotlib.collections as mcoll import matplotlib.font_manager as font_manager -import matplotlib.text as text import matplotlib.cbook as cbook import matplotlib.patches as mpatches import matplotlib.transforms as mtransforms @@ -31,7 +31,7 @@ # per level. -class ClabelText(text.Text): +class ClabelText(Text): """ Unlike the ordinary text, the get_rotation returns an updated angle in the pixel coordinate assuming that the input rotation is @@ -253,11 +253,10 @@ def _get_nth_label_width(self, nth): fig = self.axes.figure renderer = fig._get_renderer() return ( - text.Text(0, 0, - self.get_text(self.labelLevelList[nth], self.labelFmt), - figure=fig, - size=self.labelFontSizeList[nth], - fontproperties=self.labelFontProps) + Text(0, 0, self.get_text(self.labelLevelList[nth], self.labelFmt), + figure=fig, + size=self.labelFontSizeList[nth], + fontproperties=self.labelFontProps) .get_window_extent(renderer).width) @_api.deprecated("3.5") @@ -267,8 +266,8 @@ def get_label_width(self, lev, fmt, fsize): lev = self.get_text(lev, fmt) fig = self.axes.figure renderer = fig._get_renderer() - width = (text.Text(0, 0, lev, figure=fig, - size=fsize, fontproperties=self.labelFontProps) + width = (Text(0, 0, lev, figure=fig, + size=fsize, fontproperties=self.labelFontProps) .get_window_extent(renderer).width) width *= 72 / fig.dpi return width @@ -419,10 +418,9 @@ def calc_label_rot_and_inline(self, slc, ind, lw, lc=None, spacing=5): def _get_label_text(self, x, y, rotation): dx, dy = self.axes.transData.inverted().transform((x, y)) - t = text.Text(dx, dy, rotation=rotation, - horizontalalignment='center', - verticalalignment='center', zorder=self._clabel_zorder) - return t + return Text(dx, dy, rotation=rotation, + horizontalalignment='center', + verticalalignment='center', zorder=self._clabel_zorder) def _get_label_clabeltext(self, x, y, rotation): # x, y, rotation is given in pixel coordinate. Convert them to @@ -585,6 +583,10 @@ def labels(self, inline, inline_spacing): if inline: paths[:] = additions + def remove(self): + for text in self.labelTexts: + text.remove() + def _is_closed_polygon(X): """ @@ -1389,6 +1391,11 @@ def find_nearest_contour(self, x, y, indices=None, pixel=True): return (conmin, segmin, imin, xmin, ymin, d2min) + def remove(self): + super().remove() + for coll in self.collections: + coll.remove() + @_docstring.dedent_interpd class QuadContourSet(ContourSet): diff --git a/lib/matplotlib/tests/test_contour.py b/lib/matplotlib/tests/test_contour.py index 2c76f34cb180..a8cd656b1fe5 100644 --- a/lib/matplotlib/tests/test_contour.py +++ b/lib/matplotlib/tests/test_contour.py @@ -682,3 +682,13 @@ def test_negative_linestyles(style): ax4.clabel(CS4, fontsize=9, inline=True) ax4.set_title(f'Single color - negative contours {style}') assert CS4.negative_linestyles == style + + +def test_contour_remove(): + ax = plt.figure().add_subplot() + orig_children = ax.get_children() + cs = ax.contour(np.arange(16).reshape((4, 4))) + cs.clabel() + assert ax.get_children() != orig_children + cs.remove() + assert ax.get_children() == orig_children