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

Lines changed: 69 additions & 84 deletions

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[:]

0 commit comments

Comments
 (0)