Skip to content

Commit 5cc7449

Browse files
committed
Rationalise artist get_figure
1 parent 9387431 commit 5cc7449

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

lib/matplotlib/artist.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def __init__(self):
181181
self._stale = True
182182
self.stale_callback = None
183183
self._axes = None
184-
self.figure = None
184+
self._figure = None
185185

186186
self._transform = None
187187
self._transformSet = False
@@ -290,6 +290,8 @@ def convert_yunits(self, y):
290290
return y
291291
return ax.yaxis.convert_units(y)
292292

293+
figure = _api.deprecate_privatize_attribute('3.10', 'get_figure or set_figure')
294+
293295
@property
294296
def axes(self):
295297
"""The `~.axes.Axes` instance the artist resides in, or *None*."""
@@ -720,9 +722,20 @@ def set_path_effects(self, path_effects):
720722
def get_path_effects(self):
721723
return self._path_effects
722724

723-
def get_figure(self):
724-
"""Return the `.Figure` instance the artist belongs to."""
725-
return self.figure
725+
def get_figure(self, root=False):
726+
"""
727+
Return the `.Figure` or `.SubFigure` instance the artist belongs to.
728+
729+
Parameters
730+
----------
731+
root : bool, default=False
732+
If False, return the (Sub)Figure this artist is on. If True,
733+
return the root Figure for a nested tree of SubFigures.
734+
"""
735+
if root:
736+
return self._figure._figure
737+
738+
return self._figure
726739

727740
def set_figure(self, fig):
728741
"""

lib/matplotlib/figure.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464

6565
def _stale_figure_callback(self, val):
66+
"""Callback used on non-figure artists."""
6667
if self.figure:
6768
self.figure.stale = val
6869

@@ -1239,11 +1240,11 @@ def colorbar(
12391240
if isinstance(mappable_host_fig, mpl.figure.SubFigure):
12401241
mappable_host_fig = mappable_host_fig.figure
12411242
# Warn in case of mismatch
1242-
if mappable_host_fig is not self.figure:
1243+
if mappable_host_fig is not self._figure:
12431244
_api.warn_external(
12441245
f'Adding colorbar to a different Figure '
12451246
f'{repr(mappable.figure)} than '
1246-
f'{repr(self.figure)} which '
1247+
f'{repr(self._figure)} which '
12471248
f'fig.colorbar is called on.')
12481249

12491250
NON_COLORBAR_KEYS = [ # remove kws that cannot be passed to Colorbar
@@ -1760,7 +1761,7 @@ def get_tightbbox(self, renderer=None, bbox_extra_artists=None):
17601761
"""
17611762

17621763
if renderer is None:
1763-
renderer = self.figure._get_renderer()
1764+
renderer = self._figure._get_renderer()
17641765

17651766
bb = []
17661767
if bbox_extra_artists is None:
@@ -2215,7 +2216,7 @@ def __init__(self, parent, subplotspec, *,
22152216

22162217
self._subplotspec = subplotspec
22172218
self._parent = parent
2218-
self.figure = parent.figure
2219+
self._figure = parent._figure
22192220

22202221
# subfigures use the parent axstack
22212222
self._axstack = parent._axstack
@@ -2239,6 +2240,13 @@ def __init__(self, parent, subplotspec, *,
22392240
self._set_artist_props(self.patch)
22402241
self.patch.set_antialiased(False)
22412242

2243+
def get_figure(self, root=False):
2244+
# docstring inherited
2245+
if root:
2246+
return self._figure
2247+
2248+
return self._parent
2249+
22422250
@property
22432251
def dpi(self):
22442252
return self._parent.dpi
@@ -2349,7 +2357,7 @@ def draw(self, renderer):
23492357
renderer.open_group('subfigure', gid=self.get_gid())
23502358
self.patch.draw(renderer)
23512359
mimage._draw_list_compositing_images(
2352-
renderer, self, artists, self.figure.suppressComposite)
2360+
renderer, self, artists, self._figure.suppressComposite)
23532361
renderer.close_group('subfigure')
23542362

23552363
finally:
@@ -2493,7 +2501,7 @@ def __init__(self,
24932501
%(Figure:kwdoc)s
24942502
"""
24952503
super().__init__(**kwargs)
2496-
self.figure = self
2504+
self._figure = self
24972505
self._layout_engine = None
24982506

24992507
if layout is not None:

0 commit comments

Comments
 (0)