Skip to content

Commit 3829417

Browse files
committed
Avoid vstack() when possible.
In transforms.py, array() is faster -- the changes speeds up the proposed precision-sensitive version of format_data_short by ~10% (essentially because we get to skip the calls to np.atleast_2d in vstack). Also replace a vstack() call by a concatenate() in path.py, while we're at it.
1 parent 95c8c88 commit 3829417

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

lib/matplotlib/path.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,8 @@ def make_compound_path(cls, *args):
330330
if not args:
331331
return Path(np.empty([0, 2], dtype=np.float32))
332332

333-
lengths = [len(x) for x in args]
334-
total_length = sum(lengths)
335-
336-
vertices = np.vstack([x.vertices for x in args])
337-
vertices.reshape((total_length, 2))
338-
339-
codes = np.empty(total_length, dtype=cls.code_type)
333+
vertices = np.concatenate([x.vertices for x in args])
334+
codes = np.empty(len(vertices), dtype=cls.code_type)
340335
i = 0
341336
for path in args:
342337
if path.codes is None:

lib/matplotlib/transforms.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,10 +2176,9 @@ def get_affine(self):
21762176
else:
21772177
x_mtx = self._x.get_affine().get_matrix()
21782178
y_mtx = self._y.get_affine().get_matrix()
2179-
# This works because we already know the transforms are
2180-
# separable, though normally one would want to set b and
2181-
# c to zero.
2182-
mtx = np.vstack((x_mtx[0], y_mtx[1], [0.0, 0.0, 1.0]))
2179+
# We already know the transforms are separable, so we can skip
2180+
# setting b and c to zero.
2181+
mtx = np.array([x_mtx[0], y_mtx[1], [0.0, 0.0, 1.0]])
21832182
self._affine = Affine2D(mtx)
21842183
self._invalid = 0
21852184
return self._affine
@@ -2229,10 +2228,9 @@ def get_matrix(self):
22292228
else:
22302229
x_mtx = self._x.get_matrix()
22312230
y_mtx = self._y.get_matrix()
2232-
# This works because we already know the transforms are
2233-
# separable, though normally one would want to set b and
2234-
# c to zero.
2235-
self._mtx = np.vstack((x_mtx[0], y_mtx[1], [0.0, 0.0, 1.0]))
2231+
# We already know the transforms are separable, so we can skip
2232+
# setting b and c to zero.
2233+
self._mtx = np.array([x_mtx[0], y_mtx[1], [0.0, 0.0, 1.0]])
22362234
self._inverted = None
22372235
self._invalid = 0
22382236
return self._mtx

0 commit comments

Comments
 (0)