@@ -2985,6 +2985,10 @@ def _update_title_position(self, renderer):
2985
2985
2986
2986
titles = (self .title , self ._left_title , self ._right_title )
2987
2987
2988
+ if not any (title .get_text () for title in titles ):
2989
+ # If the titles are all empty, there is no need to update their positions.
2990
+ return
2991
+
2988
2992
# Need to check all our twins too, aligned axes, and all the children
2989
2993
# as well.
2990
2994
axs = set ()
@@ -2996,21 +3000,24 @@ def _update_title_position(self, renderer):
2996
3000
locator = ax .get_axes_locator ()
2997
3001
ax .apply_aspect (locator (self , renderer ) if locator else None )
2998
3002
3003
+ top = - np .inf
3004
+ for ax in axs :
3005
+ bb = None
3006
+ xticklabel_top = any (tick .label2 .get_visible () for tick in
3007
+ [ax .xaxis .majorTicks [0 ], ax .xaxis .minorTicks [0 ]])
3008
+ if (xticklabel_top or ax .xaxis .get_label_position () == 'top' ):
3009
+ bb = ax .xaxis .get_tightbbox (renderer )
3010
+ if bb is None :
3011
+ # Extent of the outline for colorbars, of the axes otherwise.
3012
+ bb = ax .spines .get ("outline" , ax ).get_window_extent ()
3013
+ top = max (top , bb .ymax )
3014
+
2999
3015
for title in titles :
3000
3016
x , _ = title .get_position ()
3001
3017
# need to start again in case of window resizing
3002
3018
title .set_position ((x , 1.0 ))
3003
- top = - np .inf
3004
- for ax in axs :
3005
- bb = None
3006
- if (ax .xaxis .get_ticks_position () in ['top' , 'unknown' ]
3007
- or ax .xaxis .get_label_position () == 'top' ):
3008
- bb = ax .xaxis .get_tightbbox (renderer )
3009
- if bb is None :
3010
- # Extent of the outline for colorbars, of the axes otherwise.
3011
- bb = ax .spines .get ("outline" , ax ).get_window_extent ()
3012
- top = max (top , bb .ymax )
3013
- if title .get_text ():
3019
+ if title .get_text ():
3020
+ for ax in axs :
3014
3021
ax .yaxis .get_tightbbox (renderer ) # update offsetText
3015
3022
if ax .yaxis .offsetText .get_text ():
3016
3023
bb = ax .yaxis .offsetText .get_tightbbox (renderer )
0 commit comments