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

Skip to content

Commit fb6ca2d

Browse files
authored
Merge pull request #31004 from scottshambaugh/more_speedups
PERF: More speedups
2 parents 64cf757 + 1fe8981 commit fb6ca2d

4 files changed

Lines changed: 51 additions & 24 deletions

File tree

lib/matplotlib/axes/_base.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,15 +3014,17 @@ def autoscale_view(self, tight=None, scalex=True, scaley=True):
30143014
x_stickies = y_stickies = np.array([])
30153015
if self.use_sticky_edges:
30163016
if self._xmargin and scalex and self.get_autoscalex_on():
3017-
x_stickies = np.sort(np.concatenate([
3018-
artist.sticky_edges.x
3019-
for ax in self._shared_axes["x"].get_siblings(self)
3020-
for artist in ax.get_children()]))
3017+
edges = []
3018+
for ax in self._shared_axes["x"].get_siblings(self):
3019+
for artist in ax.get_children():
3020+
edges.extend(artist.sticky_edges.x)
3021+
x_stickies = np.sort(edges)
30213022
if self._ymargin and scaley and self.get_autoscaley_on():
3022-
y_stickies = np.sort(np.concatenate([
3023-
artist.sticky_edges.y
3024-
for ax in self._shared_axes["y"].get_siblings(self)
3025-
for artist in ax.get_children()]))
3023+
edges = []
3024+
for ax in self._shared_axes["y"].get_siblings(self):
3025+
for artist in ax.get_children():
3026+
edges.extend(artist.sticky_edges.y)
3027+
y_stickies = np.sort(edges)
30263028
if self.get_xscale() == 'log':
30273029
x_stickies = x_stickies[x_stickies > 0]
30283030
if self.get_yscale() == 'log':

lib/matplotlib/collections.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,15 @@ def _make_verts(self, t, f1, f2, where):
15111511
t, f1, f2 = np.broadcast_arrays(np.atleast_1d(t), f1, f2, subok=True)
15121512

15131513
self._bbox = transforms.Bbox.null()
1514-
self._bbox.update_from_data_xy(self._fix_pts_xy_order(np.concatenate([
1515-
np.stack((t[where], f[where]), axis=-1) for f in (f1, f2)])))
1514+
t_where = t.data[where] if np.ma.isMA(t) else t[where]
1515+
f1_where = f1.data[where] if np.ma.isMA(f1) else f1[where]
1516+
f2_where = f2.data[where] if np.ma.isMA(f2) else f2[where]
1517+
n = len(t_where)
1518+
if n > 0:
1519+
pts = np.empty((2 * n, 2)) # Preallocate and fill for speed
1520+
pts[:n, 0], pts[:n, 1] = t_where, f1_where
1521+
pts[n:, 0], pts[n:, 1] = t_where, f2_where
1522+
self._bbox.update_from_data_xy(self._fix_pts_xy_order(pts))
15161523

15171524
return [
15181525
self._make_verts_for_region(t, f1, f2, idx0, idx1)
@@ -1570,11 +1577,14 @@ def _make_verts_for_region(self, t, f1, f2, idx0, idx1):
15701577
start = t_slice[0], f2_slice[0]
15711578
end = t_slice[-1], f2_slice[-1]
15721579

1573-
pts = np.concatenate((
1574-
np.asarray([start]),
1575-
np.stack((t_slice, f1_slice), axis=-1),
1576-
np.asarray([end]),
1577-
np.stack((t_slice, f2_slice), axis=-1)[::-1]))
1580+
# Build polygon: start -> along f1 -> end -> back along f2 (reversed)
1581+
# Preallocate and fill for speed
1582+
n = len(t_slice)
1583+
pts = np.empty((2 * n + 2, 2))
1584+
pts[0, :] = start
1585+
pts[1:n+1, 0], pts[1:n+1, 1] = t_slice, f1_slice
1586+
pts[n+1, :] = end
1587+
pts[n+2:, 0], pts[n+2:, 1] = t_slice[::-1], f2_slice[::-1]
15781588

15791589
return self._fix_pts_xy_order(pts)
15801590

lib/matplotlib/lines.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,12 @@ def recache(self, always=False):
709709
interpolation_steps = self._path._interpolation_steps
710710
else:
711711
interpolation_steps = 1
712-
xy = STEP_LOOKUP_MAP[self._drawstyle](*self._xy.T)
713-
self._path = Path(np.asarray(xy).T,
714-
_interpolation_steps=interpolation_steps)
712+
if self._drawstyle == 'default':
713+
vertices = self._xy
714+
else:
715+
step_func = STEP_LOOKUP_MAP[self._drawstyle]
716+
vertices = np.asarray(step_func(*self._xy.T)).T
717+
self._path = Path(vertices, _interpolation_steps=interpolation_steps)
715718
self._transformed_path = None
716719
self._invalidx = False
717720
self._invalidy = False
@@ -724,8 +727,12 @@ def _transform_path(self, subslice=None):
724727
"""
725728
# Masked arrays are now handled by the Path class itself
726729
if subslice is not None:
727-
xy = STEP_LOOKUP_MAP[self._drawstyle](*self._xy[subslice, :].T)
728-
_path = Path(np.asarray(xy).T,
730+
if self._drawstyle == 'default':
731+
vertices = self._xy[subslice]
732+
else:
733+
step_func = STEP_LOOKUP_MAP[self._drawstyle]
734+
vertices = np.asarray(step_func(*self._xy[subslice, :].T)).T
735+
_path = Path(vertices,
729736
_interpolation_steps=self._path._interpolation_steps)
730737
else:
731738
_path = self._path

lib/matplotlib/transforms.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,14 +904,22 @@ def update_from_path(self, path, ignore=None, updatex=True, updatey=True):
904904

905905
if updatex:
906906
x = path.vertices[..., 0][valid_points]
907-
points[0, 0] = min(points[0, 0], np.min(x, initial=np.inf))
907+
minx = np.min(x, initial=np.inf)
908+
points[0, 0] = min(points[0, 0], minx)
908909
points[1, 0] = max(points[1, 0], np.max(x, initial=-np.inf))
909-
minpos[0] = min(minpos[0], np.min(x[x > 0], initial=np.inf))
910+
if minx > 0: # Fast path for all-positive x values
911+
minpos[0] = min(minpos[0], minx)
912+
else:
913+
minpos[0] = min(minpos[0], np.min(x[x > 0], initial=np.inf))
910914
if updatey:
911915
y = path.vertices[..., 1][valid_points]
912-
points[0, 1] = min(points[0, 1], np.min(y, initial=np.inf))
916+
miny = np.min(y, initial=np.inf)
917+
points[0, 1] = min(points[0, 1], miny)
913918
points[1, 1] = max(points[1, 1], np.max(y, initial=-np.inf))
914-
minpos[1] = min(minpos[1], np.min(y[y > 0], initial=np.inf))
919+
if miny > 0: # Fast path for all-positive y values
920+
minpos[1] = min(minpos[1], miny)
921+
else:
922+
minpos[1] = min(minpos[1], np.min(y[y > 0], initial=np.inf))
915923

916924
if np.any(points != self._points) or np.any(minpos != self._minpos):
917925
self.invalidate()

0 commit comments

Comments
 (0)