@@ -4099,16 +4099,33 @@ def get_default_bbox_extra_artists(self):
4099
4099
return [artist for artist in self .get_children ()
4100
4100
if artist .get_visible ()]
4101
4101
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 ):
4103
4104
"""
4104
4105
Return the tight bounding box of the axes.
4105
4106
The dimension of the Bbox in canvas coordinate.
4106
4107
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).
4112
4129
"""
4113
4130
4114
4131
bb = []
@@ -4142,11 +4159,27 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
4142
4159
if bb_yaxis :
4143
4160
bb .append (bb_yaxis )
4144
4161
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 )
4150
4183
4151
4184
_bbox = mtransforms .Bbox .union (
4152
4185
[b for b in bb if b .width != 0 or b .height != 0 ])
0 commit comments