@@ -1281,13 +1281,13 @@ def _set_artist_props(self, a):
12811281 return
12821282 a .set_figure (self .get_figure (root = False ))
12831283
1284- def _update_ticks (self , * , _use_cached = False ):
1284+ def _update_ticks (self , * , _use_cache = False ):
12851285 """
12861286 Update ticks (position and labels) using the current data interval of
12871287 the axes. Return the list of ticks that will be drawn.
12881288 """
12891289 # Return cached result if available and requested
1290- if _use_cached and self ._cached_ticks_to_draw is not None :
1290+ if _use_cache and self ._cached_ticks_to_draw is not None :
12911291 return self ._cached_ticks_to_draw
12921292
12931293 major_locs = self .get_majorticklocs ()
@@ -1335,14 +1335,15 @@ def _update_ticks(self, *, _use_cached=False):
13351335 if mtransforms ._interval_contains_close (interval_t , loc_t ):
13361336 ticks_to_draw .append (tick )
13371337
1338- # Cache the result before returning
1339- self ._cached_ticks_to_draw = ticks_to_draw
1338+ # Only cache the result when called from the draw path
1339+ if _use_cache :
1340+ self ._cached_ticks_to_draw = ticks_to_draw
13401341 return ticks_to_draw
13411342
1342- def _get_ticklabel_bboxes (self , ticks , renderer , * , _use_cached = False ):
1343+ def _get_ticklabel_bboxes (self , ticks , renderer , * , _use_cache = False ):
13431344 """Return lists of bboxes for ticks' label1's and label2's."""
13441345 # Return cached result if available and requested
1345- if _use_cached and self ._cached_ticklabel_bboxes is not None :
1346+ if _use_cache and self ._cached_ticklabel_bboxes is not None :
13461347 return self ._cached_ticklabel_bboxes
13471348
13481349 result = ([tick .label1 .get_window_extent (renderer )
@@ -1352,8 +1353,9 @@ def _get_ticklabel_bboxes(self, ticks, renderer, *, _use_cached=False):
13521353 for tick in ticks
13531354 if tick .label2 .get_visible () and tick .label2 .get_in_layout ()])
13541355
1355- # Cache the result before returning
1356- self ._cached_ticklabel_bboxes = result
1356+ # Only cache the result when called from the draw path
1357+ if _use_cache :
1358+ self ._cached_ticklabel_bboxes = result
13571359 return result
13581360
13591361 def get_tightbbox (self , renderer = None , * , for_layout_only = False ):
@@ -1373,13 +1375,13 @@ def get_tightbbox(self, renderer=None, *, for_layout_only=False):
13731375 # Don't use cached values here - get_tightbbox() is called during
13741376 # layout calculations (e.g., constrained_layout) outside of draw(),
13751377 # and must always recalculate to reflect current state.
1376- ticks_to_draw = self ._update_ticks (_use_cached = False )
1378+ ticks_to_draw = self ._update_ticks (_use_cache = False )
13771379
13781380 self ._update_label_position (renderer )
13791381
13801382 # go back to just this axis's tick labels
13811383 tlb1 , tlb2 = self ._get_ticklabel_bboxes (ticks_to_draw , renderer ,
1382- _use_cached = False )
1384+ _use_cache = False )
13831385
13841386 self ._update_offset_text_position (tlb1 , tlb2 )
13851387 self .offsetText .set_text (self .major .formatter .get_offset ())
@@ -1426,13 +1428,9 @@ def draw(self, renderer):
14261428 return
14271429 renderer .open_group (__name__ , gid = self .get_gid ())
14281430
1429- # Clear caches for this draw cycle
1430- self ._cached_ticks_to_draw = None
1431- self ._cached_ticklabel_bboxes = None
1432-
1433- ticks_to_draw = self ._update_ticks (_use_cached = True )
1431+ ticks_to_draw = self ._update_ticks (_use_cache = True )
14341432 tlb1 , tlb2 = self ._get_ticklabel_bboxes (ticks_to_draw , renderer ,
1435- _use_cached = True )
1433+ _use_cache = True )
14361434
14371435 for tick in ticks_to_draw :
14381436 tick .draw (renderer )
@@ -1448,6 +1446,10 @@ def draw(self, renderer):
14481446 renderer .close_group (__name__ )
14491447 self .stale = False
14501448
1449+ # Reset cached values for next draw cycle, in case not called by Axes.draw()
1450+ self ._cached_ticks_to_draw = None
1451+ self ._cached_ticklabel_bboxes = None
1452+
14511453 def get_gridlines (self ):
14521454 r"""Return this Axis' grid lines as a list of `.Line2D`\s."""
14531455 ticks = self .get_major_ticks ()
@@ -2268,7 +2270,7 @@ def _get_tick_boxes_siblings(self, renderer):
22682270 # If we want to align labels from other Axes:
22692271 for ax in grouper .get_siblings (self .axes ):
22702272 axis = ax ._axis_map [name ]
2271- ticks_to_draw = axis ._update_ticks (_use_cached = True )
2273+ ticks_to_draw = axis ._update_ticks (_use_cache = True )
22722274 tlb , tlb2 = axis ._get_ticklabel_bboxes (ticks_to_draw , renderer ,
22732275 _use_cached = True )
22742276 bboxes .extend (tlb )
0 commit comments