@@ -4110,19 +4110,47 @@ def pick(self, *args):
41104110 martist .Artist .pick (self , args [0 ])
41114111
41124112 def get_default_bbox_extra_artists (self ):
4113+ """
4114+ Return a default list of artists that are used for the bounding box
4115+ calculation.
4116+
4117+ Artists are excluded either by not being visible or
4118+ ``artist.set_in_layout(False)``.
4119+ """
41134120 return [artist for artist in self .get_children ()
4114- if artist .get_visible ()]
4121+ if ( artist .get_visible () and artist . get_in_layout () )]
41154122
4116- def get_tightbbox (self , renderer , call_axes_locator = True ):
4123+ def get_tightbbox (self , renderer , call_axes_locator = True ,
4124+ bbox_extra_artists = None ):
41174125 """
4118- Return the tight bounding box of the axes.
4119- The dimension of the Bbox in canvas coordinate.
4126+ Return the tight bounding box of the axes, including axis and their
4127+ decorators (xlabel, title, etc).
4128+
4129+ Artists that have ``artist.set_in_layout(False)`` are not included
4130+ in the bbox.
4131+
4132+ Parameters
4133+ ----------
4134+ renderer : `.RendererBase` instance
4135+ renderer that will be used to draw the figures (i.e.
4136+ ``fig.canvas.get_renderer()``)
4137+
4138+ bbox_extra_artists : list of `.Artist` or ``None``
4139+ List of artists to include in the tight bounding box. If
4140+ ``None`` (default), then all artist children of the axes are
4141+ included in the tight bounding box.
4142+
4143+ call_axes_locator : boolean (default ``True``)
4144+ If *call_axes_locator* is ``False``, it does not call the
4145+ ``_axes_locator`` attribute, which is necessary to get the correct
4146+ bounding box. ``call_axes_locator=False`` can be used if the
4147+ caller is only interested in the relative size of the tightbbox
4148+ compared to the axes bbox.
41204149
4121- If *call_axes_locator* is *False*, it does not call the
4122- _axes_locator attribute, which is necessary to get the correct
4123- bounding box. ``call_axes_locator==False`` can be used if the
4124- caller is only intereted in the relative size of the tightbbox
4125- compared to the axes bbox.
4150+ Returns
4151+ -------
4152+ bbox : `.BboxBase`
4153+ bounding box in figure pixel coordinates.
41264154 """
41274155
41284156 bb = []
@@ -4155,11 +4183,14 @@ def get_tightbbox(self, renderer, call_axes_locator=True):
41554183 if bb_yaxis :
41564184 bb .append (bb_yaxis )
41574185
4158- for child in self .get_children ():
4159- if isinstance (child , OffsetBox ) and child .get_visible ():
4160- bb .append (child .get_window_extent (renderer ))
4161- elif isinstance (child , Legend ) and child .get_visible ():
4162- bb .append (child ._legend_box .get_window_extent (renderer ))
4186+ bbox_artists = bbox_extra_artists
4187+ if bbox_artists is None :
4188+ bbox_artists = self .get_default_bbox_extra_artists ()
4189+
4190+ for a in bbox_artists :
4191+ bbox = a .get_tightbbox (renderer )
4192+ if bbox is not None and (bbox .width != 0 or bbox .height != 0 ):
4193+ bb .append (bbox )
41634194
41644195 _bbox = mtransforms .Bbox .union (
41654196 [b for b in bb if b .width != 0 or b .height != 0 ])
0 commit comments