Skip to content

Commit 3cab932

Browse files
committed
MNT: Track if we ever set the Axes or Figure when cleared
closes matplotlib#6793
1 parent a844eca commit 3cab932

3 files changed

Lines changed: 84 additions & 15 deletions

File tree

‎lib/matplotlib/artist.py‎

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
_log = logging.getLogger(__name__)
2222

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

2427
def _prevent_rasterization(draw):
2528
# We assume that by default artists are not allowed to rasterize (unless
@@ -181,7 +184,7 @@ def __init__(self):
181184
self._stale = True
182185
self.stale_callback = None
183186
self._axes = None
184-
self.figure = None
187+
self._figure = None
185188

186189
self._transform = None
187190
self._transformSet = False
@@ -295,19 +298,60 @@ def convert_yunits(self, y):
295298
@property
296299
def axes(self):
297300
"""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
299302

300303
@axes.setter
301304
def axes(self, new_axes):
302305
if (new_axes is not None and self._axes is not None
303306
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
307324
self._axes = new_axes
308325
if new_axes is not None and new_axes is not self:
309326
self.stale_callback = _stale_axes_callback
310327

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+
311355
@property
312356
def stale(self):
313357
"""
@@ -739,14 +783,6 @@ def set_figure(self, fig):
739783
# if this is a no-op just return
740784
if self.figure is fig:
741785
return
742-
# if we currently have a figure (the case of both `self.figure`
743-
# and *fig* being none is taken care of above) we then user is
744-
# trying to change the figure an artist is associated with which
745-
# is not allowed for the same reason as adding the same instance
746-
# to more than one Axes
747-
if self.figure is not None:
748-
raise RuntimeError("Can not put single artist in "
749-
"more than one figure")
750786
self.figure = fig
751787
if self.figure and self.figure is not self:
752788
self.pchanged()

‎lib/matplotlib/figure.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def __init__(self, **kwargs):
188188
# axis._get_tick_boxes_siblings
189189
self._align_label_groups = {"x": cbook.Grouper(), "y": cbook.Grouper()}
190190

191-
self.figure = self
191+
self._figure = self
192192
self._localaxes = [] # track all axes
193193
self.artists = []
194194
self.lines = []
@@ -2178,7 +2178,7 @@ def __init__(self, parent, subplotspec, *,
21782178

21792179
self._subplotspec = subplotspec
21802180
self._parent = parent
2181-
self.figure = parent.figure
2181+
self._figure = parent.figure
21822182
self._fig_callbacks = parent._fig_callbacks
21832183

21842184
# subfigures use the parent axstack

‎lib/matplotlib/tests/test_artist.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,36 @@ def draw(self, renderer, extra):
562562

563563
assert 'aardvark' == art.draw(renderer, 'aardvark')
564564
assert 'aardvark' == art.draw(renderer, extra='aardvark')
565+
566+
567+
def test_no_reset():
568+
fig, (ax1, ax2) = plt.subplots(2)
569+
fig2 = plt.figure()
570+
ln, = ax1.plot(range(5))
571+
with pytest.raises(
572+
ValueError, match=f"The artist {ln!r} is currently in {ax1!r}"
573+
):
574+
ax2.add_line(ln)
575+
with pytest.raises(
576+
ValueError, match=f"The artist {ln!r} is currently in {ax1!r}"
577+
):
578+
ln.axes = ax2
579+
with pytest.raises(
580+
ValueError, match=f"The artist {ln!r} is currently in {fig!r}"
581+
):
582+
ln.figure = fig2
583+
584+
ln.remove()
585+
assert ln.figure is None
586+
assert ln.axes is None
587+
with pytest.raises(
588+
ValueError, match=f"The artist {ln!r} has had its Figure reset "
589+
):
590+
ax2.add_line(ln)
591+
with pytest.raises(
592+
ValueError, match=f"The artist {ln!r} has had its Axes reset "
593+
):
594+
ln.axes = ax2
595+
596+
assert ln.axes is None
597+
assert ln.figure is None

0 commit comments

Comments
 (0)