Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1022fe9

Browse files
committed
Remove redundant iteration through Axes._children.
1 parent d5e1218 commit 1022fe9

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,10 +1886,13 @@ def _sci(self, im):
18861886
`~.pyplot.viridis`, and other functions such as `~.pyplot.clim`. The
18871887
current image is an attribute of the current axes.
18881888
"""
1889+
cbook._check_isinstance(
1890+
(mpl.contour.ContourSet, mcoll.Collection, mimage.AxesImage),
1891+
im=im)
18891892
if isinstance(im, mpl.contour.ContourSet):
1890-
if im.collections[0] not in self.collections:
1893+
if im.collections[0] not in self._children:
18911894
raise ValueError("ContourSet must be in current Axes")
1892-
elif im not in self.images and im not in self.collections:
1895+
elif im not in self._children:
18931896
raise ValueError("Argument must be an image, collection, or "
18941897
"ContourSet in this Axes")
18951898
self._current_image = im
@@ -1906,11 +1909,9 @@ def has_data(self):
19061909
need to be updated, and may not actually be useful for
19071910
anything.
19081911
"""
1909-
return (
1910-
len(self.collections) +
1911-
len(self.images) +
1912-
len(self.lines) +
1913-
len(self.patches)) > 0
1912+
return any(isinstance(a, (mcoll.Collection, mimage.AxesImage,
1913+
mlines.Line2D, mpatches.Patch))
1914+
for a in self._children)
19141915

19151916
def add_artist(self, a):
19161917
"""
@@ -2145,17 +2146,14 @@ def relim(self, visible_only=False):
21452146
self.dataLim.set_points(mtransforms.Bbox.null().get_points())
21462147
self.ignore_existing_data_limits = True
21472148

2148-
for line in self.lines:
2149-
if not visible_only or line.get_visible():
2150-
self._update_line_limits(line)
2151-
2152-
for p in self.patches:
2153-
if not visible_only or p.get_visible():
2154-
self._update_patch_limits(p)
2155-
2156-
for image in self.images:
2157-
if not visible_only or image.get_visible():
2158-
self._update_image_limits(image)
2149+
for artist in self._children:
2150+
if not visible_only or artist.get_visible():
2151+
if isinstance(artist, mlines.Line2D):
2152+
self._update_line_limits(artist)
2153+
elif isinstance(artist, mpatches.Patch):
2154+
self._update_patch_limits(artist)
2155+
elif isinstance(artist, mimage.AxesImage):
2156+
self._update_image_limits(artist)
21592157

21602158
def update_datalim(self, xys, updatex=True, updatey=True):
21612159
"""

lib/matplotlib/legend.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
from matplotlib.font_manager import FontProperties
3535
from matplotlib.lines import Line2D
3636
from matplotlib.patches import Patch, Rectangle, Shadow, FancyBboxPatch
37-
from matplotlib.collections import (LineCollection, RegularPolyCollection,
38-
CircleCollection, PathCollection,
39-
PolyCollection)
37+
from matplotlib.collections import (
38+
Collection, CircleCollection, LineCollection, PathCollection,
39+
PolyCollection, RegularPolyCollection)
4040
from matplotlib.transforms import Bbox, BboxBase, TransformedBbox
4141
from matplotlib.transforms import BboxTransformTo, BboxTransformFrom
4242

@@ -831,18 +831,23 @@ def _auto_legend_data(self):
831831
List of (x, y) offsets of all collection.
832832
"""
833833
assert self.isaxes # always holds, as this is only called internally
834-
ax = self.parent
835-
lines = [line.get_transform().transform_path(line.get_path())
836-
for line in ax.lines]
837-
bboxes = [patch.get_bbox().transformed(patch.get_data_transform())
838-
if isinstance(patch, Rectangle) else
839-
patch.get_path().get_extents(patch.get_transform())
840-
for patch in ax.patches]
834+
bboxes = []
835+
lines = []
841836
offsets = []
842-
for handle in ax.collections:
843-
_, transOffset, hoffsets, _ = handle._prepare_points()
844-
for offset in transOffset.transform(hoffsets):
845-
offsets.append(offset)
837+
for artist in self.parent._children:
838+
if isinstance(artist, Line2D):
839+
lines.append(
840+
artist.get_transform().transform_path(artist.get_path()))
841+
elif isinstance(artist, Rectangle):
842+
bboxes.append(
843+
artist.get_bbox().transformed(artist.get_data_transform()))
844+
elif isinstance(artist, Patch):
845+
bboxes.append(
846+
artist.get_path().get_extents(artist.get_transform()))
847+
elif isinstance(artist, Collection):
848+
_, transOffset, hoffsets, _ = artist._prepare_points()
849+
for offset in transOffset.transform(hoffsets):
850+
offsets.append(offset)
846851
return bboxes, lines, offsets
847852

848853
def get_children(self):
@@ -1119,13 +1124,17 @@ def _get_legend_handles(axs, legend_handler_map=None):
11191124
"""
11201125
handles_original = []
11211126
for ax in axs:
1122-
handles_original += [*ax.lines, *ax.patches, *ax.collections,
1123-
*ax.containers]
1127+
handles_original += [
1128+
*(a for a in ax._children
1129+
if isinstance(a, (Line2D, Patch, Collection))),
1130+
*ax.containers]
11241131
# support parasite axes:
11251132
if hasattr(ax, 'parasites'):
11261133
for axx in ax.parasites:
1127-
handles_original += [*axx.lines, *axx.patches,
1128-
*axx.collections, *axx.containers]
1134+
handles_original += [
1135+
*(a for a in axx._children
1136+
if isinstance(a, (Line2D, Patch, Collection))),
1137+
*axx.containers]
11291138

11301139
handler_map = Legend.get_default_handler_map()
11311140

0 commit comments

Comments
 (0)