@@ -2371,14 +2371,25 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
23712371 if tight is not None :
23722372 self ._tight = bool (tight )
23732373
2374- if self .use_sticky_edges and (
2375- (self ._xmargin and scalex and self ._autoscaleXon ) or
2376- (self ._ymargin and scaley and self ._autoscaleYon )):
2377- stickies = [artist .sticky_edges for artist in self .get_children ()]
2378- else : # Small optimization.
2379- stickies = []
2380- x_stickies = np .sort ([x for sticky in stickies for x in sticky .x ])
2381- y_stickies = np .sort ([y for sticky in stickies for y in sticky .y ])
2374+ x_stickies = y_stickies = np .array ([])
2375+ if self .use_sticky_edges :
2376+ # Only iterate over axes and artists if needed. The check for
2377+ # ``hasattr(ax, "lines")`` is necessary because this can be called
2378+ # very early in the axes init process (e.g., for twin axes) when
2379+ # these attributes don't even exist yet, in which case
2380+ # `get_children` would raise an AttributeError.
2381+ if self ._xmargin and scalex and self ._autoscaleXon :
2382+ x_stickies = np .sort (np .concatenate ([
2383+ artist .sticky_edges .x
2384+ for ax in self ._shared_x_axes .get_siblings (self )
2385+ if hasattr (ax , "lines" )
2386+ for artist in ax .get_children ()]))
2387+ if self ._ymargin and scaley and self ._autoscaleYon :
2388+ y_stickies = np .sort (np .concatenate ([
2389+ artist .sticky_edges .y
2390+ for ax in self ._shared_y_axes .get_siblings (self )
2391+ if hasattr (ax , "lines" )
2392+ for artist in ax .get_children ()]))
23822393 if self .get_xscale ().lower () == 'log' :
23832394 x_stickies = x_stickies [x_stickies > 0 ]
23842395 if self .get_yscale ().lower () == 'log' :
0 commit comments