@@ -4091,16 +4091,33 @@ def get_default_bbox_extra_artists(self):
4091
4091
return [artist for artist in self .get_children ()
4092
4092
if artist .get_visible ()]
4093
4093
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 ):
4095
4096
"""
4096
4097
Return the tight bounding box of the axes.
4097
4098
The dimension of the Bbox in canvas coordinate.
4098
4099
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).
4104
4121
"""
4105
4122
4106
4123
bb = []
@@ -4132,11 +4149,27 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
4132
4149
if bb_yaxis :
4133
4150
bb .append (bb_yaxis )
4134
4151
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 )
4140
4173
4141
4174
_bbox = mtransforms .Bbox .union (
4142
4175
[b for b in bb if b .width != 0 or b .height != 0 ])
0 commit comments