Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31dab4c

Browse files
committed
ENH have ax.get_tightbbox have a bbox around all artists
1 parent 5df8380 commit 31dab4c

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

lib/matplotlib/axes/_base.py

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

4094-
def get_tightbbox(self, renderer, call_axes_locator=True):
4094+
def get_tightbbox(self, renderer, call_axes_locator=True,
4095+
bbox_extra_artists=None):
40954096
"""
40964097
Return the tight bounding box of the axes.
40974098
The dimension of the Bbox in canvas coordinate.
40984099
4099-
If *call_axes_locator* is *False*, it does not call the
4100-
_axes_locator attribute, which is necessary to get the correct
4101-
bounding box. ``call_axes_locator==False`` can be used if the
4102-
caller is only intereted in the relative size of the tightbbox
4103-
compared to the axes bbox.
4100+
Parameters
4101+
----------
4102+
renderer : RendererBase instance
4103+
renderer that will be used to draw the figures (i.e.
4104+
``fig.canvas.get_renderer()``)
4105+
4106+
bbox_extra_artists : list of `Artist` or ``None``
4107+
List of artists to include in the tight bounding box. If
4108+
``None`` (default), then all artist children of the axes are
4109+
included in the tight bounding box.
4110+
4111+
call_axes_locator : boolean (default ``True``)
4112+
If *call_axes_locator* is ``False``, it does not call the
4113+
``_axes_locator`` attribute, which is necessary to get the correct
4114+
bounding box. ``call_axes_locator=False`` can be used if the
4115+
caller is only interested in the relative size of the tightbbox
4116+
compared to the axes bbox.
4117+
4118+
Returns
4119+
-------
4120+
`BboxBase` : containing the bounding box (in relative co-ordinates).
41044121
"""
41054122

41064123
bb = []
@@ -4132,11 +4149,27 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
41324149
if bb_yaxis:
41334150
bb.append(bb_yaxis)
41344151

4135-
for child in self.get_children():
4136-
if isinstance(child, OffsetBox) and child.get_visible():
4137-
bb.append(child.get_window_extent(renderer))
4138-
elif isinstance(child, Legend) and child.get_visible():
4139-
bb.append(child._legend_box.get_window_extent(renderer))
4152+
bbox_artists = bbox_extra_artists
4153+
if bbox_artists is None:
4154+
bbox_artists = self.get_default_bbox_extra_artists()
4155+
4156+
for a in bbox_artists:
4157+
if isinstance(a, Legend) and a.get_visible():
4158+
bb.append(a._legend_box.get_window_extent(renderer))
4159+
else:
4160+
bbox = a.get_window_extent(renderer)
4161+
if a.get_clip_on():
4162+
clip_box = a.get_clip_box()
4163+
if clip_box is not None:
4164+
bbox = mtransforms.Bbox.intersection(bbox, clip_box)
4165+
clip_path = a.get_clip_path()
4166+
if clip_path is not None and bbox is not None:
4167+
clip_path = clip_path.get_fully_transformed_path()
4168+
bbox = mtransforms.Bbox.intersection(bbox,
4169+
clip_path.get_extents())
4170+
if bbox is not None and (bbox.width != 0 or
4171+
bbox.height != 0):
4172+
bb.append(bbox)
41404173

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

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,33 +2180,9 @@ def print_figure(self, filename, dpi=None, facecolor=None, edgecolor=None,
21802180
dryrun=True,
21812181
**kwargs)
21822182
renderer = self.figure._cachedRenderer
2183-
bbox_inches = self.figure.get_tightbbox(renderer)
2184-
21852183
bbox_artists = kwargs.pop("bbox_extra_artists", None)
2186-
if bbox_artists is None:
2187-
bbox_artists = self.figure.get_default_bbox_extra_artists()
2188-
2189-
bbox_filtered = []
2190-
for a in bbox_artists:
2191-
bbox = a.get_window_extent(renderer)
2192-
if a.get_clip_on():
2193-
clip_box = a.get_clip_box()
2194-
if clip_box is not None:
2195-
bbox = Bbox.intersection(bbox, clip_box)
2196-
clip_path = a.get_clip_path()
2197-
if clip_path is not None and bbox is not None:
2198-
clip_path = clip_path.get_fully_transformed_path()
2199-
bbox = Bbox.intersection(bbox,
2200-
clip_path.get_extents())
2201-
if bbox is not None and (bbox.width != 0 or
2202-
bbox.height != 0):
2203-
bbox_filtered.append(bbox)
2204-
2205-
if bbox_filtered:
2206-
_bbox = Bbox.union(bbox_filtered)
2207-
trans = Affine2D().scale(1.0 / self.figure.dpi)
2208-
bbox_extra = TransformedBbox(_bbox, trans)
2209-
bbox_inches = Bbox.union([bbox_inches, bbox_extra])
2184+
bbox_inches = self.figure.get_tightbbox(renderer,
2185+
bbox_artists)
22102186

22112187
pad = kwargs.pop("pad_inches", None)
22122188
if pad is None:

lib/matplotlib/figure.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,18 +2215,31 @@ def get_default_bbox_extra_artists(self):
22152215
bbox_artists.remove(self.patch)
22162216
return bbox_artists
22172217

2218-
def get_tightbbox(self, renderer):
2218+
def get_tightbbox(self, renderer, bbox_extra_artists=None):
22192219
"""
22202220
Return a (tight) bounding box of the figure in inches.
22212221
2222-
Currently, this takes only axes title, axis labels, and axis
2223-
ticklabels into account. Needs improvement.
2222+
Parameters
2223+
----------
2224+
renderer : RendererBase instance
2225+
renderer that will be used to draw the figures (i.e.
2226+
``fig.canvas.get_renderer()``)
2227+
2228+
bbox_extra_artists : list of `Artist` or ``None``
2229+
List of artists to include in the tight bounding box. If
2230+
``None`` (default), then all artist children of each axes are
2231+
included in the tight bounding box.
2232+
2233+
Returns
2234+
-------
2235+
`BboxBase` : containing the bounding box (in figure co-ordinates).
22242236
"""
22252237

2226-
bb = []
2238+
bb = [artist for artist in self.get_children()
2239+
if artist.get_visible()]
22272240
for ax in self.axes:
22282241
if ax.get_visible():
2229-
bb.append(ax.get_tightbbox(renderer))
2242+
bb.append(ax.get_tightbbox(renderer, bbox_extra_artists))
22302243

22312244
if len(bb) == 0:
22322245
return self.bbox_inches

0 commit comments

Comments
 (0)