10000 Remove list(zip(...)) when unnecessary. · matplotlib/matplotlib@0754b29 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0754b29

Browse files
committed
Remove list(zip(...)) when unnecessary.
Replace by zip(...) when the result is immediately unpacked. Replace by np.column_stack when operating on arrays or when the result will be immediately converted to an array (e.g. by the Path constructor), as column_stack is faster in these cases. (This commit does not remove *all* instances of list(zip(...)).)
1 parent 355943e commit 0754b29

23 files changed

+61
-65
lines changed

examples/api/collections.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@
3030
theta = np.linspace(0, 2*np.pi, nverts)
3131
xx = r * np.sin(theta)
3232
yy = r * np.cos(theta)
33-
spiral = list(zip(xx, yy))
33+
spiral = np.column_stack([xx, yy])
3434

3535
# Make some offsets
3636
# Fixing random state for reproducibility
3737
rs = np.random.RandomState(19680801)
3838

3939

40-
xo = rs.randn(npts)
41-
yo = rs.randn(npts)
42-
xyo = list(zip(xo, yo))
40+
xyo = rs.randn(npts, 2)
4341

4442
# Make a list of colors cycling through the default series.
4543
colors = [colors.to_rgba(c)
@@ -109,11 +107,11 @@
109107

110108
yy = np.linspace(0, 2*np.pi, nverts)
111109
ym = np.max(yy)
112-
xx = (0.2 + (ym - yy)/ym)**2 * np.cos(yy - 0.4)*0.5
110+
xx = (0.2 + (ym - yy) / ym) ** 2 * np.cos(yy - 0.4) * 0.5
113111
segs = []
114112
for i in range(ncurves):
115113
xxx = xx + 0.02*rs.randn(nverts)
116-
curve = list(zip(xxx, yy*100))
114+
curve = np.column_stack([xxx, yy * 100])
117115
segs.append(curve)
118116

119117
col = collections.LineCollection(segs, offsets=offs)

examples/api/scatter_piecharts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@
2424
# some points on a circle cos,sin
2525
x = [0] + np.cos(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
2626
y = [0] + np.sin(np.linspace(0, 2 * np.pi * r1, 10)).tolist()
27-
xy1 = list(zip(x, y))
27+
xy1 = np.column_stack([x, y])
2828
s1 = np.abs(xy1).max()
2929

3030
x = [0] + np.cos(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
3131
y = [0] + np.sin(np.linspace(2 * np.pi * r1, 2 * np.pi * r2, 10)).tolist()
32-
xy2 = list(zip(x, y))
32+
xy2 = np.column_stack([x, y])
3333
s2 = np.abs(xy2).max()
3434

3535
x = [0] + np.cos(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
3636
y = [0] + np.sin(np.linspace(2 * np.pi * r2, 2 * np.pi, 10)).tolist()
37-
xy3 = list(zip(x, y))
37+
xy3 = np.column_stack([x, y])
3838
s3 = np.abs(xy3).max()
3939

4040
fig, ax = plt.subplots()

examples/event_handling/poly_editor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,10 @@ def motion_notify_callback(self, event):
167167
theta = np.arange(0, 2*np.pi, 0.1)
168168
r = 1.5
169169

170-
xs = r*np.cos(theta)
171-
ys = r*np.sin(theta)
170+
xs = r * np.cos(theta)
171+
ys = r * np.sin(theta)
172172

173-
poly = Polygon(list(zip(xs, ys)), animated=True)
173+
poly = Polygon(np.column_stack([xs, ys]), animated=True)
174174

175175
fig, ax = plt.subplots()
176176
ax.add_patch(poly)

examples/event_handling/trifinder_event_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def update_polygon(tri):
2020
points = triang.triangles[tri]
2121
xs = triang.x[points]
2222
ys = triang.y[points]
23-
polygon.set_xy(list(zip(xs, ys)))
23+
polygon.set_xy(np.column_stack([xs, ys]))
2424

2525

2626
def motion_notify(event):

examples/lines_bars_and_markers/scatter_custom_symbol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
rx, ry = 3., 1.
1212
area = rx * ry * np.pi
1313
theta = np.arange(0, 2 * np.pi + 0.01, 0.1)
14-
verts = list(zip(rx / area * np.cos(theta), ry / area * np.sin(theta)))
14+
verts = np.column_stack([rx / area * np.cos(theta), ry / area * np.sin(theta)])
1515

1616
x, y, s, c = np.random.rand(4, 30)
1717
s *= 10**2.

examples/lines_bars_and_markers/scatter_star_poly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
plt.subplot(322)
2222
plt.scatter(x, y, s=80, c=z, marker=(5, 0))
2323

24-
verts = list(zip([-1., 1., 1., -1.], [-1., -1., 1., -1.]))
24+
verts = np.array([[-1, -1], [1, -1], [1, 1], [-1, -1]])
2525
plt.subplot(323)
2626
plt.scatter(x, y, s=80, c=z, marker=(verts, 0))
2727
# equivalent:

examples/shapes_and_collections/line_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
# See :class:`matplotlib.collections.LineCollection` for more information
7373

7474
# Make a sequence of x,y pairs
75-
line_segments = LineCollection([list(zip(x, y)) for y in ys],
75+
line_segments = LineCollection([np.column_stack([x, y]) for y in ys],
7676
linewidths=(0.5, 1, 1.5, 2),
7777
linestyles='solid')
7878
line_segments.set_array(x)

examples/statistics/boxplot_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
for j in range(5):
137137
boxX.append(box.get_xdata()[j])
138138
boxY.append(box.get_ydata()[j])
139-
boxCoords = list(zip(boxX, boxY))
139+
boxCoords = np.column_stack([boxX, boxY])
140140
# Alternate between Dark Khaki and Royal Blue
141141
k = i % 2
142142
boxPolygon = Polygon(boxCoords, facecolor=boxColors[k])

lib/matplotlib/axes/_axes.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,12 +3675,11 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
36753675
final_meanprops.update(meanprops)
36763676

36773677
def to_vc(xs, ys):
3678-
# convert arguments to verts and codes
3679-
verts = list(zip(xs, ys))
3680-
verts.append((0, 0)) # ignored
3681-
codes = [mpath.Path.MOVETO] + \
3682-
[mpath.Path.LINETO] * (len(verts) - 2) + \
3683-
[mpath.Path.CLOSEPOLY]
3678+
# convert arguments to verts and codes, append (0, 0) (ignored).
3679+
verts = np.append(np.column_stack([xs, ys]), [(0, 0)], 0)
3680+
codes = ([mpath.Path.MOVETO]
3681+
+ [mpath.Path.LINETO] * (len(verts) - 2)
3682+
+ [mpath.Path.CLOSEPOLY])
36843683
return verts, codes
36853684

36863685
def patch_list(xs, ys, **kwargs):

lib/matplotlib/bezier.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,9 @@ def split_path_inout(path, inside, tolerence=0.01, reorder_inout=False):
265265
if bezier_path is None:
266266
raise ValueError("The path does not seem to intersect with the patch")
267267

268-
bp = list(zip(bezier_path[::2], bezier_path[1::2]))
269-
left, right = split_bezier_intersecting_with_closedpath(bp,
270-
inside,
271-
tolerence)
268+
bp = np.column_stack([bezier_path[::2], bezier_path[1::2]])
269+
left, right = split_bezier_intersecting_with_closedpath(
270+
bp, inside, tolerence)
272271
if len(left) == 2:
273272
codes_left = [Path.LINETO]
274273
codes_right = [Path.MOVETO, Path.LINETO]
@@ -279,7 +278,7 @@ def split_path_inout(path, inside, tolerence=0.01, reorder_inout=False):
279278
codes_left = [Path.CURVE4, Path.CURVE4, Path.CURVE4]
280279
codes_right = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4]
281280
else:
282-
raise ValueError()
281+
raise ValueError
283282

284283
verts_left = left[1:]
285284
verts_right = right[:]

lib/matplotlib/offsetbox.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ def get_extent_offsets(self, renderer):
398398
ydescent = height - yoffsets[0]
399399
yoffsets = height - yoffsets
400400

401-
#w, h, xd, h_yd = whd_list[-1]
402401
yoffsets = yoffsets - ydescent
403402

404403
return width + 2 * pad, height + 2 * pad, \

lib/matplotlib/patches.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ def get_path(self):
13571357
xs = self.convert_xunits([xb1, xb2, xc2, xd2, x1, xd1, xc1, xb1])
13581358
ys = self.convert_yunits([yb1, yb2, yc2, yd2, y1, yd1, yc1, yb1])
13591359

1360-
return Path(list(zip(xs, ys)), closed=True)
1360+
return Path(np.column_stack([xs, ys]), closed=True)
13611361

13621362
def get_patch_transform(self):
13631363
return transforms.IdentityTransform()

lib/matplotlib/sankey.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def add(self, patchlabel='', flows=None, orientations=None, labels='',
704704
[(Path.CLOSEPOLY, urpath[0][1])])
705705

706706
# Create a patch with the Sankey outline.
707-
codes, vertices = list(zip(*path))
707+
codes, vertices = zip(*path)
708708
vertices = np.array(vertices)
709709

710710
def _get_angle(a, r):

lib/matplotlib/tests/test_axes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,8 @@ def test_scatter_marker():
17051705
rx, ry = 3, 1
17061706
area = rx * ry * np.pi
17071707
theta = np.linspace(0, 2 * np.pi, 21)
1708-
verts = list(zip(np.cos(theta) * rx / area, np.sin(theta) * ry / area))
1708+
verts = np.column_stack([np.cos(theta) * rx / area,
1709+
np.sin(theta) * ry / area])
17091710
ax2.scatter([3, 4, 2, 6], [2, 5, 2, 3],
17101711
c=[(1, 0, 0), 'y', 'b', 'lime'],
17111712
s=[60, 50, 40, 30],

lib/matplotlib/tests/test_category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def test_plot_update(self):
239239

240240
labels = ['a', 'b', 'd', 'c']
241241
ticks = [0, 1, 2, 3]
242-
unit_data = MockUnitData(list(zip(labels, ticks)))
242+
unit_data = MockUnitData(zip(labels, ticks))
243243

244244
self.axis_test(ax.yaxis, ticks, labels, unit_data)
245245

lib/matplotlib/tests/test_streamplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_startpoints():
2929
X, Y, U, V = velocity_field()
3030
start_x = np.linspace(X.min(), X.max(), 10)
3131
start_y = np.linspace(Y.min(), Y.max(), 10)
32-
start_points = list(zip(start_x, start_y))
32+
start_points = np.column_stack([start_x, start_y])
3333
plt.streamplot(X, Y, U, V, start_points=start_points)
3434
plt.plot(start_x, start_y, 'ok')
3535

lib/matplotlib/tests/test_transforms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def test_clipping_of_log():
197197
clip=(0, 0, 100, 100),
198198
simplify=False)
199199

200-
tpoints, tcodes = list(zip(*result))
200+
tpoints, tcodes = zip(*result)
201201
assert_allclose(tcodes, [M, L, L, L, C])
202202

203203

lib/mpl_toolkits/axisartist/clip_path.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ def clip_line_to_rect(xline, yline, bbox):
125125

126126
#c_left = [((x, y), (a+90)%180-180) for (x, y, a) in c_left_ \
127127
# if bbox.containsy(y)]
128-
c_left = [((x, y), (a+90)%180-90) for (x, y, a) in c_left_ \
128+
c_left = [((x, y), (a+90)%180-90) for (x, y, a) in c_left_
129129
if bbox.containsy(y)]
130-
c_bottom = [((x, y), (90 - a)%180) for (y, x, a) in c_bottom_ \
130+
c_bottom = [((x, y), (90 - a)%180) for (y, x, a) in c_bottom_
131131
if bbox.containsx(x)]
132-
c_right = [((x, y), (a+90)%180+90) for (x, y, a) in c_right_ \
132+
c_right = [((x, y), (a+90)%180+90) for (x, y, a) in c_right_
133133
if bbox.containsy(y)]
134-
c_top = [((x, y), (90 - a)%180+180) for (y, x, a) in c_top_ \
134+
c_top = [((x, y), (90 - a)%180+180) for (y, x, a) in c_top_
135135
if bbox.containsx(x)]
136136

137137
return list(zip(lx4, ly4)), [c_left, c_bottom, c_right, c_top]

lib/mpl_toolkits/axisartist/floating_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ def get_line(self, axes):
214214
top=("lat_lines0", 1))[self._side]
215215

216216
xx, yy = self.grid_info[k][v]
217-
return Path(list(zip(xx, yy)))
217+
return Path(np.column_stack([xx, yy]))
218218

219219

220220

lib/mpl_toolkits/axisartist/grid_helper_curvelinear.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def get_line(self, axes):
318318
x, y = self.grid_info["line_xy"]
319319

320320
if self._get_line_path is None:
321-
return Path(list(zip(x, y)))
321+
return Path(np.column_stack([x, y]))
322322
else:
323323
return self._get_line_path(axes, x, y)
324324

lib/mpl_toolkits/mplot3d/art3d.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ def do_3d_projection(self, renderer):
226226
xyslist = [
227227
proj3d.proj_trans_points(points, renderer.M) for points in
228228
self._segments3d]
229-
segments_2d = [list(zip(xs, ys)) for (xs, ys, zs) in xyslist]
229+
segments_2d = [np.column_stack([xs, ys]) for xs, ys, zs in xyslist]
230230
LineCollection.set_segments(self, segments_2d)
231231

232232
# FIXME
233233
minz = 1e9
234-
for (xs, ys, zs) in xyslist:
234+
for xs, ys, zs in xyslist:
235235
minz = min(minz, min(zs))
236236
return minz
237237

@@ -273,9 +273,9 @@ def get_facecolor(self):
273273

274274
def do_3d_projection(self, renderer):
275275
s = self._segment3d
276-
xs, ys, zs = list(zip(*s))
277-
vxs, vys,vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
278-
self._path2d = mpath.Path(list(zip(vxs, vys)))
276+
xs, ys, zs = zip(*s)
277+
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
278+
self._path2d = mpath.Path(np.column_stack([vxs, vys]))
279279
# FIXME: coloring
280280
self._facecolor2d = self._facecolor3d
281281
return min(vzs)
@@ -301,9 +301,9 @@ def set_3d_properties(self, path, zs=0, zdir='z'):
301301

302302
def do_3d_projection(self, renderer):
303303
s = self._segment3d
304-
xs, ys, zs = list(zip(*s))
305-
vxs, vys,vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
306-
self._path2d = mpath.Path(list(zip(vxs, vys)), self._code3d)
304+
xs, ys, zs = zip(*s)
305+
vxs, vys, vzs, vis = proj3d.proj_transform_clip(xs, ys, zs, renderer.M)
306+
self._path2d = mpath.Path(np.column_stack([vxs, vys]), self._code3d)
307307
# FIXME: coloring
308308
self._facecolor2d = self._facecolor3d
309309
return min(vzs)
@@ -375,7 +375,7 @@ def set_3d_properties(self, zs, zdir):
375375
self.update_scalarmappable()
376376
offsets = self.get_offsets()
377377
if len(offsets) > 0:
378-
xs, ys = list(zip(*offsets))
378+
xs, ys = zip(*offsets)
379379
else:
380380
xs = []
381381
ys = []
@@ -397,7 +397,7 @@ def do_3d_projection(self, renderer):
397397
self._edgecolor3d)
398398
ecs = mcolors.to_rgba_array(ecs, self._alpha)
399399
self.set_edgecolors(ecs)
400-
PatchCollection.set_offsets(self, list(zip(vxs, vys)))
400+
PatchCollection.set_offsets(self, np.column_stack([vxs, vys]))
401401

402402
if vzs.size > 0:
403403
return min(vzs)
@@ -443,7 +443,7 @@ def set_3d_properties(self, zs, zdir):
443443
self.update_scalarmappable()
444444
offsets = self.get_offsets()
445445
if len(offsets) > 0:
446-
xs, ys = list(zip(*offsets))
446+
xs, ys = zip(*offsets)
447447
else:
448448
xs = []
449449
ys = []
@@ -465,7 +465,7 @@ def do_3d_projection(self, renderer):
465465
self._edgecolor3d)
466466
ecs = mcolors.to_rgba_array(ecs, self._alpha)
467467
self.set_edgecolors(ecs)
468-
PathCollection.set_offsets(self, list(zip(vxs, vys)))
468+
PathCollection.set_offsets(self, np.column_stack([vxs, vys]))
469469

470470
if vzs.size > 0 :
471471
return min(vzs)
@@ -558,12 +558,12 @@ def get_vector(self, segments3d):
558558
points = []
559559
for p in segments3d:
560560
points.extend(p)
561-
ei = si+len(p)
561+
ei = si + len(p)
562562
segis.append((si, ei))
563563
si = ei
564564

565-
if len(segments3d) > 0 :
566-
xs, ys, zs = list(zip(*points))
565+
if len(segments3d):
566+
xs, ys, zs = zip(*points)
567567
else :
568568
# We need this so that we can skip the bad unpacking from zip()
569569
xs, ys, zs = [], [], []
@@ -630,7 +630,7 @@ def do_3d_projection(self, renderer):
630630
# if required sort by depth (furthest drawn first)
631631
if self._zsort:
632632
z_segments_2d = sorted(
633-
((self._zsortfunc(zs), list(zip(xs, ys)), fc, ec, idx)
633+
((self._zsortfunc(zs), np.column_stack([xs, ys]), fc, ec, idx)
634634
for idx, ((xs, ys, zs), fc, ec)
635635
in enumerate(zip(xyzlist, cface, cedge))),
636636
key=lambda x: x[0], reverse=True)

lib/mpl_toolkits/mplot3d/axes3d.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ def plot_wireframe(self, X, Y, Z, *args, **kwargs):
18871887

18881888
# If the inputs were empty, then just
18891889
# reset everything.
1890-
if Z.size == 0 :
1890+
if Z.size == 0:
18911891
rii = []
18921892
cii = []
18931893

@@ -1899,10 +1899,10 @@ def plot_wireframe(self, X, Y, Z, *args, **kwargs):
18991899
tylines = [tY[i] for i in cii]
19001900
tzlines = [tZ[i] for i in cii]
19011901

1902-
lines = [list(zip(xl, yl, zl)) for xl, yl, zl in \
1903-
zip(xlines, ylines, zlines)]
1904-
lines += [list(zip(xl, yl, zl)) for xl, yl, zl in \
1905-
zip(txlines, tylines, tzlines)]
1902+
lines = ([list(zip(xl, yl, zl))
1903+
for xl, yl, zl in zip(xlines, ylines, zlines)]
1904+
+ [list(zip(xl, yl, zl))
1905+
for xl, yl, zl in zip(txlines, tylines, tzlines)])
19061906

19071907
linec = art3d.Line3DCollection(lines, *args, **kwargs)
19081908
self.add_collection(linec)

lib/mpl_toolkits/mplot3d/proj3d.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,14 @@ def proj_transform_clip(xs, ys, zs, M):
182182
transform = proj_transform
183183

184184
def proj_points(points, M):
185-
return list(zip(*proj_trans_points(points, M)))
185+
return np.column_stack(proj_trans_points(points, M))
186186

187187
def proj_trans_points(points, M):
188-
xs, ys, zs = list(zip(*points))
188+
xs, ys, zs = zip(*points)
189189
return proj_transform(xs, ys, zs, M)
190190

191191
def proj_trans_clip_points(points, M):
192-
xs, ys, zs = list(zip(*points))
192+
xs, ys, zs = zip(*points)
193193
return proj_transform_clip(xs, ys, zs, M)
194194

195195

0 commit comments

Comments
 (0)
0