Skip to content

Commit af6fce7

Browse files
committed
Move Axes.lines into a hidden children attribute.
The lines can still be accessed via a read-only property.
1 parent 7f9d84a commit af6fce7

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,9 @@ def axline(self, xy1, xy2=None, *, slope=None, **kwargs):
866866
if line.get_clip_path() is None:
867867
line.set_clip_path(self.patch)
868868
if not line.get_label():
869-
line.set_label(f"_line{len(self.lines)}")
870-
self.lines.append(line)
871-
line._remove_method = self.lines.remove
869+
line.set_label(f"_child{len(self._children)}")
870+
self._children.append(line)
871+
line._remove_method = self._children.remove
872872
self.update_datalim(datalim)
873873

874874
self._request_autoscale_view()

lib/matplotlib/axes/_base.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,9 @@ def _update_transScale(self):
984984
self.transScale.set(
985985
mtransforms.blended_transform_factory(
986986
self.xaxis.get_transform(), self.yaxis.get_transform()))
987-
for line in getattr(self, "lines", []): # Not set during init.
987+
for line in getattr(self, "_children", []): # Not set during init.
988+
if not isinstance(line, mlines.Line2D):
989+
continue
988990
try:
989991
line._transformed_path.invalidate()
990992
except AttributeError:
@@ -1228,7 +1230,7 @@ def cla(self):
12281230
self._get_patches_for_fill = _process_plot_var_args(self, 'fill')
12291231

12301232
self._gridOn = mpl.rcParams['axes.grid']
1231-
self.lines = []
1233+
self._children = []
12321234
self.patches = []
12331235
self.texts = []
12341236
self.tables = []
@@ -1307,6 +1309,10 @@ def cla(self):
13071309

13081310
self.stale = True
13091311

1312+
@property
1313+
def lines(self):
1314+
return tuple(a for a in self._children if isinstance(a, mlines.Line2D))
1315+
13101316
def clear(self):
13111317
"""Clear the axes."""
13121318
self.cla()
@@ -2096,17 +2102,17 @@ def _update_image_limits(self, image):
20962102

20972103
def add_line(self, line):
20982104
"""
2099-
Add a `.Line2D` to the axes' lines; return the line.
2105+
Add a `.Line2D` to the Axes; return the line.
21002106
"""
21012107
self._set_artist_props(line)
21022108
if line.get_clip_path() is None:
21032109
line.set_clip_path(self.patch)
21042110

21052111
self._update_line_limits(line)
21062112
if not line.get_label():
2107-
line.set_label('_line%d' % len(self.lines))
2108-
self.lines.append(line)
2109-
line._remove_method = self.lines.remove
2113+
line.set_label(f'_child{len(self._children)}')
2114+
self._children.append(line)
2115+
line._remove_method = self._children.remove
21102116
self.stale = True
21112117
return line
21122118

@@ -2672,21 +2678,21 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
26722678
x_stickies = y_stickies = np.array([])
26732679
if self.use_sticky_edges:
26742680
# Only iterate over axes and artists if needed. The check for
2675-
# ``hasattr(ax, "lines")`` is necessary because this can be called
2676-
# very early in the axes init process (e.g., for twin axes) when
2677-
# these attributes don't even exist yet, in which case
2681+
# ``hasattr(ax, "_children")`` is necessary because this can be
2682+
# called very early in the axes init process (e.g., for twin axes)
2683+
# when these attributes don't even exist yet, in which case
26782684
# `get_children` would raise an AttributeError.
26792685
if self._xmargin and scalex and self._autoscaleXon:
26802686
x_stickies = np.sort(np.concatenate([
26812687
artist.sticky_edges.x
26822688
for ax in self._shared_x_axes.get_siblings(self)
2683-
if hasattr(ax, "lines")
2689+
if hasattr(ax, "_children")
26842690
for artist in ax.get_children()]))
26852691
if self._ymargin and scaley and self._autoscaleYon:
26862692
y_stickies = np.sort(np.concatenate([
26872693
artist.sticky_edges.y
26882694
for ax in self._shared_y_axes.get_siblings(self)
2689-
if hasattr(ax, "lines")
2695+
if hasattr(ax, "_children")
26902696
for artist in ax.get_children()]))
26912697
if self.get_xscale() == 'log':
26922698
x_stickies = x_stickies[x_stickies > 0]
@@ -4326,7 +4332,7 @@ def get_children(self):
43264332
return [
43274333
*self.collections,
43284334
*self.patches,
4329-
*self.lines,
4335+
*self._children,
43304336
*self.texts,
43314337
*self.artists,
43324338
*self.spines.values(),

lib/matplotlib/legend.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,13 +1114,13 @@ def _get_legend_handles(axs, legend_handler_map=None):
11141114
"""
11151115
handles_original = []
11161116
for ax in axs:
1117-
handles_original += (ax.lines + ax.patches +
1118-
ax.collections + ax.containers)
1117+
handles_original += [*ax.lines, *ax.patches, *ax.collections,
1118+
*ax.containers]
11191119
# support parasite axes:
11201120
if hasattr(ax, 'parasites'):
11211121
for axx in ax.parasites:
1122-
handles_original += (axx.lines + axx.patches +
1123-
axx.collections + axx.containers)
1122+
handles_original += [*axx.lines, *axx.patches,
1123+
*axx.collections, *axx.containers]
11241124

11251125
handler_map = Legend.get_default_handler_map()
11261126

0 commit comments

Comments
 (0)