8000 ENH have ax.get_tightbbox have a bbox around all artists · matplotlib/matplotlib@3159d8b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3159d8b

Browse files
committed
ENH have ax.get_tightbbox have a bbox around all artists
1 parent cc4d9ce commit 3159d8b

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4099,16 +4099,33 @@ def get_default_bbox_extra_artists(self):
40994099
return [artist for artist in self.get_children()
41004100
if artist.get_visible()]
41014101

4102-
def get_tightbbox(self, renderer, call_axes_locator=True):
4102+
def get_tightbbox(self, renderer, call_axes_locator=True,
4103+
bbox_extra_artists=None):
41034104
"""
41044105
Return the tight bounding box of the axes.
41054106
The dimension of the Bbox in canvas coordinate.
41064107
4107-
If *call_axes_locator* is *False*, it does not call the
4108-
_axes_locator attribute, which is necessary to get the correct
4109-
bounding box. ``call_axes_locator==False`` can be used if the
4110-
caller is only intereted in the relative size of the tightbbox
4111-
compared to the axes bbox.
4108+
Parameters
4109+
----------
4110+
renderer : RendererBase instance
4111+
renderer that will be used to draw the figures (i.e.
4112+
``fig.canvas.get_renderer()``)
4113+
4114+
bbox_extra_artists : list of `Artist` or ``None``
4115+
List of artists to include in the tight bounding box. If
4116+
``None`` (default), then all artist children of the axes are
4117+
included in the tight bounding box.
4118+
4119+
call_axes_locator : boolean (default ``True``)
4120+
If *call_axes_locator* is ``False``, it does not call the
4121+
``_axes_locator`` attribute, which is necessary to get the correct
4122+
bounding box. ``call_axes_locator=False`` can be used if the
4123+
caller is only interested in the relative size of the tightbbox
4124+
compared to the axes bbox.
4125+
4126+
Returns
4127+
-------
4128+
`BboxBase` : containing the bounding box (in relative co-ordinates).
41124129
"""
41134130

41144131
bb = []
@@ -4142,11 +4159,27 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
41424159
if bb_yaxis:
41434160
bb.append(bb_yaxis)
41444161

4145-
for child in self.get_children():
4146-
if isinstance(child, OffsetBox) and child.get_visible():
4147-
bb.append(child.get_window_extent(renderer))
4148-
elif isinstance(child, Legend) and child.get_visible():
4149-
bb.append(child._legend_box.get_window_extent(renderer))
4162+
bbox_artists = bbox_extra_artists
4163+
if bbox_artists is None:
4164+
bbox_artists = self.get_default_bbox_extra_artists()
4165+
4166+
for a in bbox_artists:
4167+
if isinstance(a, Legend) and a.get_visible():
4168+
bb.append(a._legend_box.get_window_extent(renderer))
4169+
else:
4170+
bbox = a.get_window_extent(renderer)
4171+
if a.get_clip_on():
4172+
clip_box = a.get_clip_box()
4173+
if clip_box is not None:
4174+
bbox = mtransforms.Bbox.intersection(bbox, clip_box)
4175+
clip_path = a.get_clip_path()
4176+
if clip_path is not None and bbox is not None:
4177+
clip_path = clip_path.get_fully_transformed_path()
4178+
bbox = mtransforms.Bbox.intersection(bbox,
4179+
clip_path.get_extents())
4180+
if bbox is not None and (bbox.width != 0 or
4181+
bbox.height != 0):
4182+
bb.append(bbox)
41504183

41514184
_bbox = mtransforms.Bbox.union(
41524185
[b for b in bb if b.width != 0 or b.height != 0])

lib/matplotlib/figure.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,18 +2001,31 @@ def get_default_bbox_extra_artists(self):
20012001
bbox_artists.remove(self.patch)
20022002
return bbox_artists
20032003

2004-
def get_tightbbox(self, renderer):
2004+
def get_tightbbox(self, renderer, bbox_extra_artists=None):
20052005
"""
20062006
Return a (tight) bounding box of the figure in inches.
20072007
2008-
Currently, this takes only axes title, axis labels, and axis
2009-
ticklabels into account. Needs improvement.
2008+
Parameters
2009+
----------
2010+
renderer : RendererBase instance
2011+
renderer that will be used to draw the figures (i.e.
2012+
``fig.canvas.get_renderer()``)
2013+
2014+
bbox_extra_artists : list of `Artist` or ``None``
2015+
List of artists to include in the tight bounding box. If
2016+
``None`` (default), then all artist children of each axes are
2017+
included in the tight bounding box.
2018+
2019+
Returns
2020+
-------
2021+
`BboxBase` : containing the bounding box (in figure co-ordinates).
20102022
"""
20112023

2012-
bb = []
2024+
bb = [artist for artist in self.get_children()
2025+
if artist.get_visible()]
20132026
for ax in self.axes:
20142027
if ax.get_visible():
2015-
bb.append(ax.get_tightbbox(renderer))
2028+
bb.append(ax.get_tightbbox(renderer, bbox_extra_artists))
20162029

20172030
if len(bb) == 0:
20182031
return self.bbox_inches

0 commit comments

Comments
 (0)
0