@@ -673,6 +673,8 @@ def __init__(self, axes, *, pickradius=15, clear=True):
673673 self ._major_tick_kw = dict ()
674674 self ._minor_tick_kw = dict ()
675675
676+ self ._cached_ticks_to_draw = None
677+
676678 if clear :
677679 self .clear ()
678680 else :
@@ -1302,11 +1304,15 @@ def _tick_group_visible(kw):
13021304 kw .get ('label2On' ) is not False or
13031305 kw .get ('gridOn' ) is not False )
13041306
1305- def _update_ticks (self ):
1307+ def _update_ticks (self , * , _use_cached = False ):
13061308 """
13071309 Update ticks (position and labels) using the current data interval of
13081310 the axes. Return the list of ticks that will be drawn.
13091311 """
1312+ # Return cached result if available and requested
1313+ if _use_cached and self ._cached_ticks_to_draw is not None :
1314+ return self ._cached_ticks_to_draw
1315+
13101316 # Check if major ticks should be computed.
13111317 # Skip if using NullLocator or if all visible components are off.
13121318 if (self ._tick_group_visible (self ._major_tick_kw )
@@ -1367,6 +1373,8 @@ def _update_ticks(self):
13671373 if mtransforms ._interval_contains_close (interval_t , loc_t ):
13681374 ticks_to_draw .append (tick )
13691375
1376+ # Cache the result before returning
1377+ self ._cached_ticks_to_draw = ticks_to_draw
13701378 return ticks_to_draw
13711379
13721380 def _get_ticklabel_bboxes (self , ticks , renderer ):
@@ -1392,7 +1400,10 @@ def get_tightbbox(self, renderer=None, *, for_layout_only=False):
13921400 return
13931401 if renderer is None :
13941402 renderer = self .get_figure (root = True )._get_renderer ()
1395- ticks_to_draw = self ._update_ticks ()
1403+ # Don't use cached values here - get_tightbbox() is called during
1404+ # layout calculations (e.g., constrained_layout) outside of draw(),
1405+ # and must always recalculate to reflect current state.
1406+ ticks_to_draw = self ._update_ticks (_use_cached = False )
13961407
13971408 self ._update_label_position (renderer )
13981409
@@ -1444,7 +1455,10 @@ def draw(self, renderer):
14441455 return
14451456 renderer .open_group (__name__ , gid = self .get_gid ())
14461457
1447- ticks_to_draw = self ._update_ticks ()
1458+ # Clear tick cache for this draw cycle
1459+ self ._cached_ticks_to_draw = None
1460+
1461+ ticks_to_draw = self ._update_ticks (_use_cached = True )
14481462 tlb1 , tlb2 = self ._get_ticklabel_bboxes (ticks_to_draw , renderer )
14491463
14501464 for tick in ticks_to_draw :
@@ -2281,7 +2295,7 @@ def _get_tick_boxes_siblings(self, renderer):
22812295 # If we want to align labels from other Axes:
22822296 for ax in grouper .get_siblings (self .axes ):
22832297 axis = ax ._axis_map [name ]
2284- ticks_to_draw = axis ._update_ticks ()
2298+ ticks_to_draw = axis ._update_ticks (_use_cached = True )
22852299 tlb , tlb2 = axis ._get_ticklabel_bboxes (ticks_to_draw , renderer )
22862300 bboxes .extend (tlb )
22872301 bboxes2 .extend (tlb2 )
0 commit comments