@@ -2436,14 +2436,25 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
24362436 if tight is not None :
24372437 self ._tight = bool (tight )
24382438
2439- if self .use_sticky_edges and (
2440- (self ._xmargin and scalex and self ._autoscaleXon ) or
2441- (self ._ymargin and scaley and self ._autoscaleYon )):
2442- stickies = [artist .sticky_edges for artist in self .get_children ()]
2443- else : # Small optimization.
2444- stickies = []
2445- x_stickies = np .sort ([x for sticky in stickies for x in sticky .x ])
2446- y_stickies = np .sort ([y for sticky in stickies for y in sticky .y ])
2439+ x_stickies = y_stickies = np .array ([])
2440+ if self .use_sticky_edges :
2441+ # Only iterate over axes and artists if needed. The check for
2442+ # ``hasattr(ax, "lines")`` is necessary because this can be called
2443+ # very early in the axes init process (e.g., for twin axes) when
2444+ # these attributes don't even exist yet, in which case
2445+ # `get_children` would raise an AttributeError.
2446+ if self ._xmargin and scalex and self ._autoscaleXon :
2447+ x_stickies = np .sort (np .concatenate ([
2448+ artist .sticky_edges .x
2449+ for ax in self ._shared_x_axes .get_siblings (self )
2450+ if hasattr (ax , "lines" )
2451+ for artist in ax .get_children ()]))
2452+ if self ._ymargin and scaley and self ._autoscaleYon :
2453+ y_stickies = np .sort (np .concatenate ([
2454+ artist .sticky_edges .y
2455+ for ax in self ._shared_y_axes .get_siblings (self )
2456+ if hasattr (ax , "lines" )
2457+ for artist in ax .get_children ()]))
24472458 if self .get_xscale ().lower () == 'log' :
24482459 x_stickies = x_stickies [x_stickies > 0 ]
24492460 if self .get_yscale ().lower () == 'log' :
0 commit comments