Skip to content

Commit 7c895b6

Browse files
committed
MNT: Track if we ever set the Axes or Figure when cleared
closes matplotlib#6793
1 parent e752f9c commit 7c895b6

2 files changed

Lines changed: 79 additions & 6 deletions

File tree

‎lib/matplotlib/artist.py‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
_log = logging.getLogger(__name__)
2121

22+
# a tombstone sentinel to ensure we never reset the axes or figure on an Artist
23+
_tombstone = object()
24+
2225

2326
def _prevent_rasterization(draw):
2427
# We assume that by default artists are not allowed to rasterize (unless
@@ -264,7 +267,7 @@ def remove(self):
264267
if (fig := self.get_figure(root=False)) is not None:
265268
if not _ax_flag:
266269
fig.stale = True
267-
self._parent_figure = None
270+
self._parent_figure = _tombstone
268271

269272
else:
270273
raise NotImplementedError('cannot remove artist')
@@ -301,18 +304,35 @@ def convert_yunits(self, y):
301304
@property
302305
def axes(self):
303306
"""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
305308

306309
@axes.setter
307310
def axes(self, new_axes):
308311
if (new_axes is not None and self._axes is not None
309312
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
312331
self._axes = new_axes
313332
if new_axes is not None and new_axes is not self:
314333
self.stale_callback = _stale_axes_callback
315334

335+
316336
@property
317337
def stale(self):
318338
"""
@@ -759,6 +779,9 @@ def get_figure(self, root=False):
759779
If False, return the (Sub)Figure this artist is on. If True,
760780
return the root Figure for a nested tree of SubFigures.
761781
"""
782+
if self._parent_figure is _tombstone:
783+
return None
784+
762785
if root and self._parent_figure is not None:
763786
return self._parent_figure.get_figure(root=True)
764787

@@ -794,8 +817,24 @@ def set_figure(self, fig):
794817
# is not allowed for the same reason as adding the same instance
795818
# to more than one Axes
796819
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+
)
799838
self._parent_figure = fig
800839
if self._parent_figure and self._parent_figure is not self:
801840
self.pchanged()

‎lib/matplotlib/tests/test_artist.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,3 +623,37 @@ def test_get_figure():
623623
assert ax.figure is sfig2
624624
assert fig.figure is fig
625625
assert sfig2.figure is fig
626+
627+
628+
def test_no_reset():
629+
fig, (ax1, ax2) = plt.subplots(2)
630+
fig2 = plt.figure()
631+
ln, = ax1.plot(range(5))
632+
with pytest.raises(
633+
RuntimeError, match=f"The artist {ln!r} is currently in {ax1!r}"
634+
):
635+
ax2.add_line(ln)
636+
with pytest.raises(
637+
RuntimeError, match=f"The artist {ln!r} is currently in {ax1!r}"
638+
):
639+
ln.axes = ax2
640+
with pytest.raises(
641+
RuntimeError, match=f"The artist {ln!r} is currently in {fig!r}"
642+
):
643+
ln.figure = fig2
644+
645+
ln.remove()
646+
assert ln.figure is None
647+
assert ln.axes is None
648+
with pytest.raises(
649+
RuntimeError, match=f"The artist {ln!r} has had its Figure reset "
650+
):
651+
ax2.add_line(ln)
652+
653+
with pytest.raises(
654+
RuntimeError, match=f"The artist {ln!r} has had its Axes reset "
655+
):
656+
ln.axes = ax2
657+
658+
assert ln.axes is None
659+
assert ln.figure is None

0 commit comments

Comments
 (0)