Skip to content

Add QuadContourSet.remove. #24143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/tests/test_contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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