diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 12f9e336680d..2af1cd86795a 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -609,16 +609,14 @@ def text(self, x, y, s, fontdict=None, else: t = mtext.Text( x=x, y=y, text=s) - self._set_artist_props(t) t.update(default) if fontdict is not None: t.update(fontdict) t.update(kwargs) - self.texts.append(t) - t._remove_method = lambda h: self.texts.remove(h) t.set_clip_path(self.patch) + self._add_text(t) return t @docstring.dedent_interpd @@ -674,11 +672,9 @@ def annotate(self, *args, **kwargs): """ a = mtext.Annotation(*args, **kwargs) a.set_transform(mtransforms.IdentityTransform()) - self._set_artist_props(a) if 'clip_on' in kwargs: a.set_clip_path(self.patch) - self.texts.append(a) - a._remove_method = lambda h: self.texts.remove(h) + self._add_text(a) return a #### Lines and spans diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index e8f161ece595..3fa887d5b3b6 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -1650,6 +1650,7 @@ def add_artist(self, a): self._set_artist_props(a) a.set_clip_path(self.patch) a._remove_method = lambda h: self.artists.remove(h) + self.stale = True return a def add_collection(self, collection, autolim=True): @@ -1672,6 +1673,7 @@ def add_collection(self, collection, autolim=True): self.update_datalim(collection.get_datalim(self.transData)) collection._remove_method = lambda h: self.collections.remove(h) + self.stale = True return collection def add_image(self, image): @@ -1683,6 +1685,7 @@ def add_image(self, image): self._set_artist_props(image) self.images.append(image) image._remove_method = lambda h: self.images.remove(h) + self.stale = True return image def add_line(self, line): @@ -1701,8 +1704,19 @@ def add_line(self, line): line.set_label('_line%d' % len(self.lines)) self.lines.append(line) line._remove_method = lambda h: self.lines.remove(h) + self.stale = True return line + def _add_text(self, txt): + """ + + """ + self._set_artist_props(txt) + self.texts.append(txt) + txt._remove_method = lambda h: self.texts.remove(h) + self.stale = True + return txt + def _update_line_limits(self, line): """ Figures out the data limit of the given line, updating self.dataLim. diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index eae8c61582d1..f975ca597037 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -369,3 +369,27 @@ def test_annotation_negative_fig_coords(): xytext=[-50, 100], textcoords='figure pixels', xy=[-50, 100], xycoords='figure pixels', fontsize=32, va='top') + + +@cleanup +def test_text_stale(): + fig, (ax1, ax2) = plt.subplots(1, 2) + plt.draw_all() + assert not ax1.stale + assert not ax2.stale + assert not fig.stale + + txt1 = ax1.text(.5, .5, 'aardvark') + assert ax1.stale + assert txt1.stale + assert fig.stale + + ann1 = ax2.annotate('aardvark', xy=[.5, .5]) + assert ax2.stale + assert ann1.stale + assert fig.stale + + plt.draw_all() + assert not ax1.stale + assert not ax2.stale + assert not fig.stale