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

Skip to content

Commit 2e32436

Browse files
Prefer np.vstack().T to np.column_stack() for speed
1 parent 78a46cd commit 2e32436

12 files changed

Lines changed: 26 additions & 29 deletions

File tree

lib/matplotlib/axes/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def _make_polygon(self, axes, x, y, kw, kwargs):
385385
# modify the kwargs dictionary.
386386
self._setdefaults(default_dict, kwargs)
387387

388-
seg = mpatches.Polygon(np.column_stack((x, y)),
388+
seg = mpatches.Polygon(np.vstack((x, y)).T,
389389
facecolor=facecolor,
390390
fill=kwargs.get('fill', True),
391391
closed=kw['closed'])

lib/matplotlib/contour.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ def _process_args(self, *args, corner_mask=None, algorithm=None, **kwargs):
13351335
if (t != self.axes.transData and
13361336
any(t.contains_branch_separately(self.axes.transData))):
13371337
trans_to_data = t - self.axes.transData
1338-
pts = np.vstack([x.flat, y.flat]).T
1338+
pts = np.vstack((x.flat, y.flat)).T
13391339
transformed_pts = trans_to_data.transform(pts)
13401340
x = transformed_pts[..., 0]
13411341
y = transformed_pts[..., 1]

lib/matplotlib/lines.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,7 @@ def recache(self, always=False):
683683
else:
684684
y = self._y
685685

686-
self._xy = np.empty((max(len(x), len(y)), 2), dtype=float)
687-
self._xy[:, 0] = x # broadcast x and y to the same shape
688-
self._xy[:, 1] = y
686+
self._xy = np.vstack(np.broadcast_arrays(x, y)).T
689687
self._x = self._xy[:, 0] # views of the x and y data
690688
self._y = self._xy[:, 1]
691689

lib/matplotlib/patches.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,9 +1131,9 @@ def _update_path(self):
11311131
else: # no baseline
11321132
raise ValueError('Invalid `baseline` specified')
11331133
if self.orientation == 'vertical':
1134-
xy = np.column_stack([x, y])
1134+
xy = np.vstack((x, y)).T
11351135
else:
1136-
xy = np.column_stack([y, x])
1136+
xy = np.vstack((y, x)).T
11371137
verts.append(xy)
11381138
codes.append([Path.MOVETO] + [Path.LINETO]*(len(xy)-1))
11391139
self._path = Path(np.concatenate(verts), np.concatenate(codes))

lib/matplotlib/path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def unit_regular_polygon(cls, numVertices):
802802
# This initial rotation is to make sure the polygon always
803803
# "points-up".
804804
+ np.pi / 2)
805-
verts = np.column_stack((np.cos(theta), np.sin(theta)))
805+
verts = np.vstack((np.cos(theta), np.sin(theta))).T
806806
path = cls(verts, closed=True, readonly=True)
807807
if numVertices <= 16:
808808
cls._unit_regular_polygons[numVertices] = path

lib/matplotlib/projections/geo.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def transform_non_affine(self, values):
271271

272272
x = (cos_latitude * np.sin(half_long)) / sinc_alpha
273273
y = np.sin(latitude) / sinc_alpha
274-
return np.column_stack([x, y])
274+
return np.vstack((x, y)).T
275275

276276
def inverted(self):
277277
# docstring inherited
@@ -313,7 +313,7 @@ def transform_non_affine(self, values):
313313
alpha = np.sqrt(1.0 + cos_latitude * np.cos(half_long))
314314
x = (2.0 * sqrt2) * (cos_latitude * np.sin(half_long)) / alpha
315315
y = (sqrt2 * np.sin(latitude)) / alpha
316-
return np.column_stack([x, y])
316+
return np.vstack((x, y)).T
317317

318318
def inverted(self):
319319
# docstring inherited
@@ -327,7 +327,7 @@ def transform_non_affine(self, values):
327327
z = np.sqrt(1 - (x / 4) ** 2 - (y / 2) ** 2)
328328
longitude = 2 * np.arctan((z * x) / (2 * (2 * z ** 2 - 1)))
329329
latitude = np.arcsin(y*z)
330-
return np.column_stack([longitude, latitude])
330+
return np.vstack((longitude, latitude)).T
331331

332332
def inverted(self):
333333
# docstring inherited
@@ -377,9 +377,8 @@ def d(theta):
377377
d = 0.5 * (3 * np.pi * e**2) ** (1.0/3)
378378
aux[ihigh] = (np.pi/2 - d) * np.sign(latitude[ihigh])
379379

380-
xy = np.empty(values.shape, dtype=float)
381-
xy[:, 0] = (2.0 * np.sqrt(2.0) / np.pi) * longitude * np.cos(aux)
382-
xy[:, 1] = np.sqrt(2.0) * np.sin(aux)
380+
xy = np.vstack(((2.0 * np.sqrt(2.0) / np.pi) * longitude * np.cos(aux),
381+
np.sqrt(2.0) * np.sin(aux))).T
383382

384383
return xy
385384

@@ -397,7 +396,7 @@ def transform_non_affine(self, values):
397396
theta = np.arcsin(y / np.sqrt(2))
398397
longitude = (np.pi / (2 * np.sqrt(2))) * x / np.cos(theta)
399398
latitude = np.arcsin((2 * theta + np.sin(2 * theta)) / np.pi)
400-
return np.column_stack([longitude, latitude])
399+
return np.vstack((longitude, latitude)).T
401400

402401
def inverted(self):
403402
# docstring inherited
@@ -446,7 +445,7 @@ def transform_non_affine(self, values):
446445
x = k * cos_lat*np.sin(diff_long)
447446
y = k * (np.cos(clat)*sin_lat - np.sin(clat)*cos_lat*cos_diff_long)
448447

449-
return np.column_stack([x, y])
448+
return np.vstack((x, y)).T
450449

451450
def inverted(self):
452451
# docstring inherited
@@ -477,7 +476,7 @@ def transform_non_affine(self, values):
477476
longitude = clong + np.arctan(
478477
(x*sin_c) / (p*np.cos(clat)*cos_c - y*np.sin(clat)*sin_c))
479478

480-
return np.column_stack([longitude, latitude])
479+
return np.vstack((longitude, latitude)).T
481480

482481
def inverted(self):
483482
# docstring inherited

lib/matplotlib/projections/polar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def transform_non_affine(self, values):
6767
if self._use_rmin and self._axis is not None:
6868
r = (r - self._get_rorigin()) * self._axis.get_rsign()
6969
r = np.where(r >= 0, r, np.nan)
70-
return np.column_stack([r * np.cos(theta), r * np.sin(theta)])
70+
return np.vstack((r * np.cos(theta), r * np.sin(theta))).T
7171

7272
def transform_path_non_affine(self, path):
7373
# docstring inherited
@@ -211,7 +211,7 @@ def transform_non_affine(self, values):
211211
if self._use_rmin and self._axis is not None:
212212
r += self._axis.get_rorigin()
213213
r *= self._axis.get_rsign()
214-
return np.column_stack([theta, r])
214+
return np.vstack((theta, r)).T
215215

216216
def inverted(self):
217217
# docstring inherited

lib/matplotlib/quiver.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def __init__(self, ax, *args,
518518
X, Y, U, V, C = _parse_args(*args, caller_name='quiver')
519519
self.X = X
520520
self.Y = Y
521-
self.XY = np.column_stack((X, Y))
521+
self.XY = np.vstack((X, Y)).T
522522
self.N = len(X)
523523
self.scale = scale
524524
self.headwidth = headwidth
@@ -638,7 +638,7 @@ def _set_transform(self):
638638
# Calculate angles and lengths for segment between (x, y), (x+u, y+v)
639639
def _angles_lengths(self, XY, U, V, eps=1):
640640
xy = self.axes.transData.transform(XY)
641-
uv = np.column_stack((U, V))
641+
uv = np.vstack((U, V)).T
642642
xyp = self.axes.transData.transform(XY + eps * uv)
643643
dxy = xyp - xy
644644
angles = np.arctan2(dxy[:, 1], dxy[:, 0])
@@ -975,7 +975,7 @@ def __init__(self, ax, *args,
975975
x, y, u, v, c = _parse_args(*args, caller_name='barbs')
976976
self.x = x
977977
self.y = y
978-
xy = np.column_stack((x, y))
978+
xy = np.vstack((x, y)).T
979979

980980
# Make a collection
981981
barb_size = self._length ** 2 / 4 # Empirically determined
@@ -1202,7 +1202,7 @@ def set_UVC(self, U, V, C=None):
12021202
self.set_array(c)
12031203

12041204
# Update the offsets in case the masked data changed
1205-
xy = np.column_stack((x, y))
1205+
xy = np.vstack((x, y)).T
12061206
self._offsets = xy
12071207
self.stale = True
12081208

@@ -1220,6 +1220,6 @@ def set_offsets(self, xy):
12201220
x, y, u, v = cbook.delete_masked_points(
12211221
self.x.ravel(), self.y.ravel(), self.u, self.v)
12221222
_check_consistent_shapes(x, y, u, v)
1223-
xy = np.column_stack((x, y))
1223+
xy = np.vstack((x, y)).T
12241224
super().set_offsets(xy)
12251225
self.stale = True

lib/matplotlib/transforms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,8 +1708,8 @@ def transform_angles(self, angles, pts, radians=False, pushoff=1e-5):
17081708
if not radians:
17091709
angles = np.deg2rad(angles)
17101710
# Move a short distance away
1711-
pts2 = pts + pushoff * np.column_stack([np.cos(angles),
1712-
np.sin(angles)])
1711+
pts2 = pts + pushoff * np.vstack((np.cos(angles),
1712+
np.sin(angles))).T
17131713
# Transform both sets of points
17141714
tpts = self.transform(pts)
17151715
tpts2 = self.transform(pts2)

lib/matplotlib/widgets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3120,7 +3120,7 @@ def set_animated(self, val):
31203120

31213121
def closest(self, x, y):
31223122
"""Return index and pixel distance to closest index."""
3123-
pts = np.column_stack([self.x, self.y])
3123+
pts = np.vstack((self.x, self.y)).T
31243124
# Transform data coordinates to pixel coordinates.
31253125
pts = self.ax.transData.transform(pts)
31263126
diff = pts - [x, y]

0 commit comments

Comments
 (0)