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

Skip to content

Commit 81b0efc

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 81b0efc

23 files changed

+69
-84
lines changed

examples/api/collections.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@
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

35-
# Make some offsets
3635
# Fixing random state for reproducibility
3736
rs = np.random.RandomState(19680801)
3837

39-
40-
xo = rs.randn(npts)
41-
yo = rs.randn(npts)
42-
xyo = list(zip(xo, yo))
38+
# Make some offsets
39+
xyo = rs.randn(npts, 2)
4340

4441
# Make a list of colors cycling through the default series.
4542
colors = [colors.to_rgba(c)
@@ -109,11 +106,11 @@
109106

110107
yy = np.linspace(0, 2*np.pi, nverts)
111108
ym = np.max(yy)
112-
xx = (0.2 + (ym - yy)/ym)**2 * np.cos(yy - 0.4)*0.5
109+
xx = (0.2 + (ym - yy) / ym) ** 2 * np.cos(yy - 0.4) * 0.5
113110
segs = []
114111
for i in range(ncurves):
115112
xxx = xx + 0.02*rs.randn(nverts)
116-
curve = list(zip(xxx, yy*100))
113+
curve = np.column_stack([xxx, yy * 100])
117114
segs.append(curve)
118115

119116
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: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,24 +168,17 @@ def __init__(self, control_points):
168168
"""
169169
_o = len(control_points)
170170
self._orders = np.arange(_o)
171-
_coeff = BezierSegment._binom_coeff[_o - 1]
172-
173-
_control_points = np.asarray(control_points)
174-
xx = _control_points[:, 0]
175-
yy = _control_points[:, 1]
176171

172+
_coeff = BezierSegment._binom_coeff[_o - 1]
173+
xx, yy = np.asarray(control_points).T
177174
self._px = xx * _coeff
178175
self._py = yy * _coeff
179176

180177
def point_at_t(self, t):
181178
"evaluate a point at t"
182-
one_minus_t_powers = np.power(1. - t, self._orders)[::-1]
183-
t_powers = np.power(t, self._orders)
184-
185-
tt = one_minus_t_powers * t_powers
186-
_x = sum(tt * self._px)
187-
_y = sum(tt * self._py)
188-
179+
tt = ((1 - t) ** self._orders)[::-1] * t ** self._orders
180+
_x = np.dot(tt, self._px)
181+
_y = np.dot(tt, self._py)
189182
return _x, _y
190183

191184

@@ -245,7 +238,6 @@ def split_path_inout(path, inside, tolerence=0.01, reorder_inout=False):
245238
ctl_points, command = next(path_iter)
246239
begin_inside = inside(ctl_points[-2:]) # true if begin point is inside
247240

248-
bezier_path = None
249241
ctl_points_old = ctl_points
250242

251243
concat = np.concatenate
@@ -259,16 +251,13 @@ def split_path_inout(path, inside, tolerence=0.01, reorder_inout=False):
259251
if inside(ctl_points[-2:]) != begin_inside:
260252
bezier_path = concat([ctl_points_old[-2:], ctl_points])
261253
break
262-
263254
ctl_points_old = ctl_points
255+
else:
256+
raise ValueError("The path does not intersect with the patch")
264257

265-
if bezier_path is None:
266-
raise ValueError("The path does not seem to intersect with the patch")
267-
268-
bp = list(zip(bezier_path[::2], bezier_path[1::2]))
269-
left, right = split_bezier_intersecting_with_closedpath(bp,
270-
inside,
271-
tolerence)
258+
bp = bezier_path.reshape((-1, 2))
259+
left, right = split_bezier_intersecting_with_closedpath(
260+
bp, inside, tolerence)
272261
if len(left) == 2:
273262
codes_left = [Path.LINETO]
274263
codes_right = [Path.MOVETO, Path.LINETO]
@@ -279,7 +268,7 @@ def split_path_inout(path, inside, tolerence=0.01, reorder_inout=False):
279268
codes_left = [Path.CURVE4, Path.CURVE4, Path.CURVE4]
280269
codes_right = [Path.MOVETO, Path.CURVE4, Path.CURVE4, Path.CURVE4]
281270
else:
282-
raise ValueError()
271+
raise AssertionError("This should never be reached")
283272

284273
verts_left = left[1:]
285274
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

0 commit comments

Comments
 (0)