|
20 | 20 |
|
21 | 21 | _log = logging.getLogger(__name__) |
22 | 22 |
|
| 23 | +# a tombstone sentinel to ensure we never reset the axes or figure on an Artist |
| 24 | +_tombstone = object() |
| 25 | + |
23 | 26 |
|
24 | 27 | def _prevent_rasterization(draw): |
25 | 28 | # We assume that by default artists are not allowed to rasterize (unless |
@@ -181,7 +184,7 @@ def __init__(self): |
181 | 184 | self._stale = True |
182 | 185 | self.stale_callback = None |
183 | 186 | self._axes = None |
184 | | - self.figure = None |
| 187 | + self._figure = None |
185 | 188 |
|
186 | 189 | self._transform = None |
187 | 190 | self._transformSet = False |
@@ -295,19 +298,60 @@ def convert_yunits(self, y): |
295 | 298 | @property |
296 | 299 | def axes(self): |
297 | 300 | """The `~.axes.Axes` instance the artist resides in, or *None*.""" |
298 | | - return self._axes |
| 301 | + return self._axes if self._axes is not _tombstone else None |
299 | 302 |
|
300 | 303 | @axes.setter |
301 | 304 | def axes(self, new_axes): |
302 | 305 | if (new_axes is not None and self._axes is not None |
303 | 306 | and new_axes != self._axes): |
304 | | - raise ValueError("Can not reset the axes. You are probably " |
305 | | - "trying to re-use an artist in more than one " |
306 | | - "Axes which is not supported") |
| 307 | + if self._axes is _tombstone: |
| 308 | + extra = ( |
| 309 | + f"The artist {self!r} has had its Axes reset to None " + |
| 310 | + "but was previously included in one." |
| 311 | + ) |
| 312 | + else: |
| 313 | + extra = ( |
| 314 | + f"The artist {self!r} is currently in {self._axes!r} " + |
| 315 | + f"in the figure {self._axes.figure!r}." |
| 316 | + ) |
| 317 | + raise ValueError("Can not reset the axes. You are probably " + |
| 318 | + "trying to re-use an artist in more than one " + |
| 319 | + "Axes which is not supported. " + |
| 320 | + extra |
| 321 | + ) |
| 322 | + if self._axes is not None and new_axes is None: |
| 323 | + new_axes = _tombstone |
307 | 324 | self._axes = new_axes |
308 | 325 | if new_axes is not None and new_axes is not self: |
309 | 326 | self.stale_callback = _stale_axes_callback |
310 | 327 |
|
| 328 | + @property |
| 329 | + def figure(self): |
| 330 | + """The `~.figure.Figure` instance the artist resides in, or *None*.""" |
| 331 | + return self._figure if self._figure is not _tombstone else None |
| 332 | + |
| 333 | + @figure.setter |
| 334 | + def figure(self, new_figure): |
| 335 | + if (new_figure is not None and self._figure is not None |
| 336 | + and new_figure != self._figure and new_figure is not True): |
| 337 | + if self._figure is _tombstone: |
| 338 | + extra = ( |
| 339 | + f"The artist {self!r} has had its Figure reset to None " + |
| 340 | + "but was previously included in one." |
| 341 | + ) |
| 342 | + else: |
| 343 | + extra = ( |
| 344 | + f"The artist {self!r} is currently in {self._figure!r}." |
| 345 | + ) |
| 346 | + raise ValueError("Can not reset the figure. You are probably " + |
| 347 | + "trying to re-use an artist in more than one " + |
| 348 | + "Figure which is not supported. " + |
| 349 | + extra |
| 350 | + ) |
| 351 | + if self._figure is not None and new_figure is None: |
| 352 | + new_figure = _tombstone |
| 353 | + self._figure = new_figure |
| 354 | + |
311 | 355 | @property |
312 | 356 | def stale(self): |
313 | 357 | """ |
@@ -745,14 +789,6 @@ def set_figure(self, fig): |
745 | 789 | # if this is a no-op just return |
746 | 790 | if self.figure is fig: |
747 | 791 | return |
748 | | - # if we currently have a figure (the case of both `self.figure` |
749 | | - # and *fig* being none is taken care of above) we then user is |
750 | | - # trying to change the figure an artist is associated with which |
751 | | - # is not allowed for the same reason as adding the same instance |
752 | | - # to more than one Axes |
753 | | - if self.figure is not None: |
754 | | - raise RuntimeError("Can not put single artist in " |
755 | | - "more than one figure") |
756 | 792 | self.figure = fig |
757 | 793 | if self.figure and self.figure is not self: |
758 | 794 | self.pchanged() |
|
0 commit comments