@@ -4086,11 +4086,15 @@ def get_default_bbox_extra_artists(self):
40864086 for _axis in self ._get_axis_list ():
40874087 artists .remove (_axis )
40884088
4089+ artists .remove (self .title )
4090+ artists .remove (self ._left_title )
4091+ artists .remove (self ._right_title )
4092+
40894093 return [artist for artist in artists
40904094 if (artist .get_visible () and artist .get_in_layout ())]
40914095
40924096 def get_tightbbox (self , renderer , call_axes_locator = True ,
4093- bbox_extra_artists = None ):
4097+ bbox_extra_artists = None , * , for_layout_only = False ):
40944098 """
40954099 Return the tight bounding box of the axes, including axis and their
40964100 decorators (xlabel, title, etc).
@@ -4116,6 +4120,10 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41164120 caller is only interested in the relative size of the tightbbox
41174121 compared to the axes bbox.
41184122
4123+ for_layout_only : default: False
4124+ The bounding box will *not* include the x-extent of the title and
4125+ the xlabel, or the y-extent of the ylabel.
4126+
41194127 Returns
41204128 -------
41214129 `.BboxBase`
@@ -4141,22 +4149,37 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41414149 self .apply_aspect ()
41424150
41434151 if self .axison :
4144- bb_xaxis = self .xaxis .get_tightbbox (renderer )
4152+ igl = 'x' if for_layout_only else None
4153+ try :
4154+ bb_xaxis = self .xaxis .get_tightbbox (renderer , ignore_label = igl )
4155+ except TypeError :
4156+ # in case downstream library has redefined axis:
4157+ bb_xaxis = self .xaxis .get_tightbbox (renderer )
41454158 if bb_xaxis :
41464159 bb .append (bb_xaxis )
41474160
4148- bb_yaxis = self .yaxis .get_tightbbox (renderer )
4161+ igl = 'y' if for_layout_only else None
4162+ try :
4163+ bb_yaxis = self .yaxis .get_tightbbox (renderer , ignore_label = igl )
4164+ except TypeError :
4165+ # in case downstream library has redefined axis:
4166+ bb_xaxis = self .yaxis .get_tightbbox (renderer )
41494167 if bb_yaxis :
41504168 bb .append (bb_yaxis )
4151-
41524169 self ._update_title_position (renderer )
4153-
41544170 axbbox = self .get_window_extent (renderer )
41554171 bb .append (axbbox )
41564172
41574173 for title in [self .title , self ._left_title , self ._right_title ]:
41584174 if title .get_visible ():
4159- bb .append (title .get_window_extent (renderer ))
4175+ bt = title .get_window_extent (renderer )
4176+ if for_layout_only and bt .width > 0 :
4177+ # make the title bbox 1 pixel wide so its width
4178+ # is not accounted for in bbox calculations in
4179+ # tight/constrained_layout
4180+ bt .x0 = (bt .x0 + bt .x1 ) / 2 - 0.5
4181+ bt .x1 = bt .x0 + 1.0
4182+ bb .append (bt )
41604183
41614184 bbox_artists = bbox_extra_artists
41624185 if bbox_artists is None :
@@ -4179,7 +4202,6 @@ def get_tightbbox(self, renderer, call_axes_locator=True,
41794202 and 0 < bbox .width < np .inf
41804203 and 0 < bbox .height < np .inf ):
41814204 bb .append (bbox )
4182-
41834205 return mtransforms .Bbox .union (
41844206 [b for b in bb if b .width != 0 or b .height != 0 ])
41854207
0 commit comments