Skip to content

Commit 26650f8

Browse files
Simplify some array allocations
1 parent 00b0ab5 commit 26650f8

2 files changed

Lines changed: 8 additions & 20 deletions

File tree

lib/matplotlib/collections.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,10 +1517,8 @@ def _make_verts(self, t, f1, f2, where):
15171517
n = len(t_where)
15181518
if n > 0:
15191519
pts = np.empty((2 * n, 2)) # Preallocate and fill for speed
1520-
pts[:n, 0] = t_where
1521-
pts[:n, 1] = f1_where
1522-
pts[n:, 0] = t_where
1523-
pts[n:, 1] = f2_where
1520+
pts[:n, 0], pts[:n, 1] = t_where, f1_where
1521+
pts[n:, 0], pts[n:, 1] = t_where[::-1], f2_where[::-1]
15241522
self._bbox.update_from_data_xy(self._fix_pts_xy_order(pts))
15251523

15261524
return [
@@ -1580,15 +1578,13 @@ def _make_verts_for_region(self, t, f1, f2, idx0, idx1):
15801578
start = t_slice[0], f2_slice[0]
15811579
end = t_slice[-1], f2_slice[-1]
15821580

1581+
# Build polygon: start -> along f1 -> end -> back along f2 (reversed)
15831582
# Preallocate and fill for speed
15841583
n = len(t_slice)
15851584
pts = np.empty((2 * n + 2, 2))
1586-
pts[0] = start
1587-
pts[1:n+1, 0] = t_slice
1588-
pts[1:n+1, 1] = f1_slice
1589-
pts[n+1] = end
1590-
pts[n+2:, 0] = t_slice[::-1]
1591-
pts[n+2:, 1] = f2_slice[::-1]
1585+
pts[0], pts[n+1] = start, end
1586+
pts[1:n+1, 0], pts[1:n+1, 1] = t_slice, f1_slice
1587+
pts[n+2:, 0], pts[n+2:, 1] = t_slice[::-1], f2_slice[::-1]
15921588

15931589
return self._fix_pts_xy_order(pts)
15941590

lib/matplotlib/lines.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,7 @@ def recache(self, always=False):
713713
if step_func is None:
714714
vertices = self._xy
715715
else:
716-
steps = step_func(*self._xy.T)
717-
# Preallocate and fill for speed
718-
vertices = np.empty((steps.shape[1], 2))
719-
vertices[:, 0] = steps[0]
720-
vertices[:, 1] = steps[1]
716+
vertices = np.asarray(step_func(*self._xy.T)).T
721717
self._path = Path(vertices, _interpolation_steps=interpolation_steps)
722718
self._transformed_path = None
723719
self._invalidx = False
@@ -735,11 +731,7 @@ def _transform_path(self, subslice=None):
735731
if step_func is None:
736732
vertices = self._xy[subslice]
737733
else:
738-
steps = step_func(*self._xy[subslice, :].T)
739-
# Preallocate and fill for speed
740-
vertices = np.empty((steps.shape[1], 2))
741-
vertices[:, 0] = steps[0]
742-
vertices[:, 1] = steps[1]
734+
vertices = np.asarray(step_func(*self._xy[subslice, :].T)).T
743735
_path = Path(vertices,
744736
_interpolation_steps=self._path._interpolation_steps)
745737
else:

0 commit comments

Comments
 (0)