|
19 | 19 |
|
20 | 20 | _log = logging.getLogger(__name__) |
21 | 21 |
|
| 22 | +# a tombstone sentinel to ensure we never reset the axes or figure on an Artist |
| 23 | +_tombstone = object() |
| 24 | + |
22 | 25 |
|
23 | 26 | def _prevent_rasterization(draw): |
24 | 27 | # We assume that by default artists are not allowed to rasterize (unless |
@@ -264,7 +267,7 @@ def remove(self): |
264 | 267 | if (fig := self.get_figure(root=False)) is not None: |
265 | 268 | if not _ax_flag: |
266 | 269 | fig.stale = True |
267 | | - self._parent_figure = None |
| 270 | + self._parent_figure = _tombstone |
268 | 271 |
|
269 | 272 | else: |
270 | 273 | raise NotImplementedError('cannot remove artist') |
@@ -301,18 +304,35 @@ def convert_yunits(self, y): |
301 | 304 | @property |
302 | 305 | def axes(self): |
303 | 306 | """The `~.axes.Axes` instance the artist resides in, or *None*.""" |
304 | | - return self._axes |
| 307 | + return self._axes if self._axes is not _tombstone else None |
305 | 308 |
|
306 | 309 | @axes.setter |
307 | 310 | def axes(self, new_axes): |
308 | 311 | if (new_axes is not None and self._axes is not None |
309 | 312 | and new_axes != self._axes): |
310 | | - raise ValueError("Can not reset the Axes. You are probably trying to reuse " |
311 | | - "an artist in more than one Axes which is not supported") |
| 313 | + if self._axes is _tombstone: |
| 314 | + extra = ( |
| 315 | + f"The artist {self!r} has had its Axes reset to None " + |
| 316 | + "but was previously included in one." |
| 317 | + ) |
| 318 | + else: |
| 319 | + extra = ( |
| 320 | + f"The artist {self!r} is currently in {self._axes!r} " + |
| 321 | + f"in the figure {self._axes.figure!r}." |
| 322 | + ) |
| 323 | + raise RuntimeError( |
| 324 | + "Can not reset the Axes. You are probably " + |
| 325 | + "trying to reuse an artist in more than one " + |
| 326 | + "Axes which is not supported. " + |
| 327 | + extra |
| 328 | + ) |
| 329 | + if self._axes is not None and new_axes is None: |
| 330 | + new_axes = _tombstone |
312 | 331 | self._axes = new_axes |
313 | 332 | if new_axes is not None and new_axes is not self: |
314 | 333 | self.stale_callback = _stale_axes_callback |
315 | 334 |
|
| 335 | + |
316 | 336 | @property |
317 | 337 | def stale(self): |
318 | 338 | """ |
@@ -759,6 +779,9 @@ def get_figure(self, root=False): |
759 | 779 | If False, return the (Sub)Figure this artist is on. If True, |
760 | 780 | return the root Figure for a nested tree of SubFigures. |
761 | 781 | """ |
| 782 | + if self._parent_figure is _tombstone: |
| 783 | + return None |
| 784 | + |
762 | 785 | if root and self._parent_figure is not None: |
763 | 786 | return self._parent_figure.get_figure(root=True) |
764 | 787 |
|
@@ -794,8 +817,24 @@ def set_figure(self, fig): |
794 | 817 | # is not allowed for the same reason as adding the same instance |
795 | 818 | # to more than one Axes |
796 | 819 | if self._parent_figure is not None: |
797 | | - raise RuntimeError("Can not put single artist in " |
798 | | - "more than one figure") |
| 820 | + if fig is None: |
| 821 | + self._parent_figure = _tombstone |
| 822 | + return |
| 823 | + if self._parent_figure is _tombstone: |
| 824 | + extra = ( |
| 825 | + f"The artist {self!r} has had its Figure reset to None " + |
| 826 | + "but was previously included in one." |
| 827 | + ) |
| 828 | + else: |
| 829 | + extra = ( |
| 830 | + f"The artist {self!r} is currently in {self._parent_figure!r}." |
| 831 | + ) |
| 832 | + raise RuntimeError( |
| 833 | + "Can not reset the figure. You are probably " + |
| 834 | + "trying to reuse an artist in more than one " + |
| 835 | + "Figure which is not supported. " + |
| 836 | + extra |
| 837 | + ) |
799 | 838 | self._parent_figure = fig |
800 | 839 | if self._parent_figure and self._parent_figure is not self: |
801 | 840 | self.pchanged() |
|
0 commit comments