Skip to content

Commit 59a3c6e

Browse files
committed
Init axes._children early enough to avoid need for some getattr calls.
1 parent ccbd641 commit 59a3c6e

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

lib/matplotlib/axes/_base.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,8 @@ def __init__(self, fig,
665665
self.set_box_aspect(box_aspect)
666666
self._axes_locator = None # Optionally set via update(kwargs).
667667

668+
self._children = []
669+
668670
# placeholder for any colorbars added that use this Axes.
669671
# (see colorbar.py):
670672
self._colorbars = []
@@ -1078,7 +1080,7 @@ def _update_transScale(self):
10781080
self.transScale.set(
10791081
mtransforms.blended_transform_factory(
10801082
self.xaxis.get_transform(), self.yaxis.get_transform()))
1081-
for line in getattr(self, "_children", []): # Not set during init.
1083+
for line in self._children:
10821084
if not isinstance(line, mlines.Line2D):
10831085
continue
10841086
try:
@@ -1291,14 +1293,6 @@ def __clear(self):
12911293
self.callbacks = cbook.CallbackRegistry(
12921294
signals=["xlim_changed", "ylim_changed", "zlim_changed"])
12931295

1294-
for name, axis in self._axis_map.items():
1295-
share = getattr(self, f"_share{name}")
1296-
if share is not None:
1297-
getattr(self, f"share{name}")(share)
1298-
else:
1299-
axis._set_scale("linear")
1300-
axis._set_lim(0, 1, auto=True)
1301-
13021296
# update the minor locator for x and y axis based on rcParams
13031297
if mpl.rcParams['xtick.minor.visible']:
13041298
self.xaxis.set_minor_locator(mticker.AutoMinorLocator())
@@ -1309,7 +1303,6 @@ def __clear(self):
13091303
self._ymargin = mpl.rcParams['axes.ymargin']
13101304
self._tight = None
13111305
self._use_sticky_edges = True
1312-
self._update_transScale() # needed?
13131306

13141307
self._get_lines = _process_plot_var_args(self)
13151308
self._get_patches_for_fill = _process_plot_var_args(self, 'fill')
@@ -1386,6 +1379,17 @@ def __clear(self):
13861379
self.yaxis.set_visible(yaxis_visible)
13871380
self.patch.set_visible(patch_visible)
13881381

1382+
# This comes last, as the call to _set_lim may trigger an autoscale (in
1383+
# case of shared axes), requiring children to be already set up.
1384+
for name, axis in self._axis_map.items():
1385+
share = getattr(self, f"_share{name}")
1386+
if share is not None:
1387+
getattr(self, f"share{name}")(share)
1388+
else:
1389+
axis._set_scale("linear")
1390+
axis._set_lim(0, 1, auto=True)
1391+
self._update_transScale()
1392+
13891393
self.stale = True
13901394

13911395
def clear(self):
@@ -2936,22 +2940,15 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
29362940

29372941
x_stickies = y_stickies = np.array([])
29382942
if self.use_sticky_edges:
2939-
# Only iterate over Axes and artists if needed. The check for
2940-
# ``hasattr(ax, "_children")`` is necessary because this can be
2941-
# called very early in the Axes init process (e.g., for twin Axes)
2942-
# when these attributes don't even exist yet, in which case
2943-
# `get_children` would raise an AttributeError.
29442943
if self._xmargin and scalex and self.get_autoscalex_on():
29452944
x_stickies = np.sort(np.concatenate([
29462945
artist.sticky_edges.x
29472946
for ax in self._shared_axes["x"].get_siblings(self)
2948-
if hasattr(ax, "_children")
29492947
for artist in ax.get_children()]))
29502948
if self._ymargin and scaley and self.get_autoscaley_on():
29512949
y_stickies = np.sort(np.concatenate([
29522950
artist.sticky_edges.y
29532951
for ax in self._shared_axes["y"].get_siblings(self)
2954-
if hasattr(ax, "_children")
29552952
for artist in ax.get_children()]))
29562953
if self.get_xscale() == 'log':
29572954
x_stickies = x_stickies[x_stickies > 0]

0 commit comments

Comments
 (0)