Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 128ee17

Browse filesBrowse files
committed
Remove/rework uses of np.where where possible.
In a few places, np.where can be replaced by direct use of boolean arrays, or other constructs. In colors.py, the use of putmask may be slightly obscure (so I left a comment explaining it), but avoids repeating the modified array twice, so I think is more legible. In polar.py, preallocating xy is just pointless given that we're constructing intermediate arrays for x and y and filling them into xy afterwards.
1 parent 72cad98 commit 128ee17
Copy full SHA for 128ee17

File tree

Expand file treeCollapse file tree

6 files changed

+24
-48
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+24
-48
lines changed

‎examples/images_contours_and_fields/barcode_demo.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/barcode_demo.py
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
# Fixing random state for reproducibility
1212
np.random.seed(19680801)
1313

14-
1514
# the bar
16-
x = np.where(np.random.rand(500) > 0.7, 1.0, 0.0)
15+
x = np.random.rand(500) > 0.7
1716

1817
axprops = dict(xticks=[], yticks=[])
19-
barprops = dict(aspect='auto', cmap=plt.cm.binary, interpolation='nearest')
18+
barprops = dict(aspect='auto', cmap='binary', interpolation='nearest')
2019

2120
fig = plt.figure()
2221

@@ -28,7 +27,6 @@
2827
ax2 = fig.add_axes([0.3, 0.1, 0.6, 0.1], **axprops)
2928
ax2.imshow(x.reshape((1, -1)), **barprops)
3029

31-
3230
plt.show()
3331

3432
#############################################################################

‎examples/images_contours_and_fields/specgram_demo.py

Copy file name to clipboardExpand all lines: examples/images_contours_and_fields/specgram_demo.py
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
Spectrogram Demo
44
================
55
6-
Demo of a spectrogram plot
7-
(:meth:`~.axes.Axes.specgram`).
6+
Demo of a spectrogram plot (`~.axes.Axes.specgram`).
87
"""
98
import matplotlib.pyplot as plt
109
import numpy as np
@@ -18,8 +17,7 @@
1817
s2 = 2 * np.sin(2 * np.pi * 400 * t)
1918

2019
# create a transient "chirp"
21-
mask = np.where(np.logical_and(t > 10, t < 12), 1.0, 0.0)
22-
s2 = s2 * mask
20+
s2[t <= 10] = s2[12 <= t] = 0
2321

2422
# add some noise into the mix
2523
nse = 0.01 * np.random.random(size=len(t))

‎examples/mplot3d/trisurf3d_2.py

Copy file name to clipboardExpand all lines: examples/mplot3d/trisurf3d_2.py
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'''
1+
"""
22
===========================
33
More triangular 3D surfaces
44
===========================
@@ -8,7 +8,7 @@
88
The first demonstrates use of plot_trisurf's triangles argument, and the
99
second sets a Triangulation object's mask and passes the object directly
1010
to plot_trisurf.
11-
'''
11+
"""
1212

1313
import numpy as np
1414
import matplotlib.pyplot as plt
@@ -71,7 +71,7 @@
7171
# Mask off unwanted triangles.
7272
xmid = x[triang.triangles].mean(axis=1)
7373
ymid = y[triang.triangles].mean(axis=1)
74-
mask = np.where(xmid**2 + ymid**2 < min_radius**2, 1, 0)
74+
mask = xmid**2 + ymid**2 < min_radius**2
7575
triang.set_mask(mask)
7676

7777
# Plot the surface.

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+13-22Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,28 +1775,19 @@ def blend_hsv(self, rgb, intensity, hsv_max_sat=None, hsv_max_val=None,
17751775
hsv = rgb_to_hsv(rgb[:, :, 0:3])
17761776

17771777
# modify hsv values to simulate illumination.
1778-
hsv[:, :, 1] = np.where(np.logical_and(np.abs(hsv[:, :, 1]) > 1.e-10,
1779-
intensity > 0),
1780-
((1. - intensity) * hsv[:, :, 1] +
1781-
intensity * hsv_max_sat),
1782-
hsv[:, :, 1])
1783-
1784-
hsv[:, :, 2] = np.where(intensity > 0,
1785-
((1. - intensity) * hsv[:, :, 2] +
1786-
intensity * hsv_max_val),
1787-
hsv[:, :, 2])
1788-
1789-
hsv[:, :, 1] = np.where(np.logical_and(np.abs(hsv[:, :, 1]) > 1.e-10,
1790-
intensity < 0),
1791-
((1. + intensity) * hsv[:, :, 1] -
1792-
intensity * hsv_min_sat),
1793-
hsv[:, :, 1])
1794-
hsv[:, :, 2] = np.where(intensity < 0,
1795-
((1. + intensity) * hsv[:, :, 2] -
1796-
intensity * hsv_min_val),
1797-
hsv[:, :, 2])
1798-
hsv[:, :, 1:] = np.where(hsv[:, :, 1:] < 0., 0, hsv[:, :, 1:])
1799-
hsv[:, :, 1:] = np.where(hsv[:, :, 1:] > 1., 1, hsv[:, :, 1:])
1778+
np.putmask(hsv[:, :, 1], # i.e. A[mask] = B[mask].
1779+
(np.abs(hsv[:, :, 1]) > 1.e-10) & (intensity > 0),
1780+
(1 - intensity) * hsv[:, :, 1] + intensity * hsv_max_sat)
1781+
np.putmask(hsv[:, :, 2],
1782+
intensity > 0,
1783+
(1 - intensity) * hsv[:, :, 2] + intensity * hsv_max_val)
1784+
np.putmask(hsv[:, :, 1],
1785+
(np.abs(hsv[:, :, 1]) > 1.e-10) & (intensity < 0),
1786+
(1 + intensity) * hsv[:, :, 1] - intensity * hsv_min_sat)
1787+
np.putmask(hsv[:, :, 2],
1788+
intensity < 0,
1789+
(1 + intensity) * hsv[:, :, 2] - intensity * hsv_min_val)
1790+
np.clip(hsv[:, :, 1:], 0, 1, out=hsv[:, :, 1:])
18001791
# convert modified hsv back to rgb.
18011792
return hsv_to_rgb(hsv)
18021793

‎lib/matplotlib/projections/polar.py

Copy file name to clipboardExpand all lines: lib/matplotlib/projections/polar.py
+3-14Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,16 @@ def __str__(self):
4444

4545
def transform_non_affine(self, tr):
4646
# docstring inherited
47-
xy = np.empty(tr.shape, float)
48-
49-
t = tr[:, 0:1]
50-
r = tr[:, 1:2]
51-
x = xy[:, 0:1]
52-
y = xy[:, 1:2]
53-
47+
t, r = np.transpose(tr)
5448
# PolarAxes does not use the theta transforms here, but apply them for
5549
# backwards-compatibility if not being used by it.
5650
if self._apply_theta_transforms and self._axis is not None:
5751
t *= self._axis.get_theta_direction()
5852
t += self._axis.get_theta_offset()
59-
6053
if self._use_rmin and self._axis is not None:
6154
r = (r - self._axis.get_rorigin()) * self._axis.get_rsign()
62-
63-
mask = r < 0
64-
x[:] = np.where(mask, np.nan, r * np.cos(t))
65-
y[:] = np.where(mask, np.nan, r * np.sin(t))
66-
67-
return xy
55+
r = np.where(r >= 0, r, np.nan)
56+
return np.column_stack([r * np.cos(t), r * np.sin(t)])
6857

6958
def transform_path_non_affine(self, path):
7059
# docstring inherited

‎lib/matplotlib/tri/triinterpolate.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tri/triinterpolate.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,7 @@ def _cg(A, b, x0=None, tol=1.e-10, maxiter=1000):
13411341
# Jacobi pre-conditioner
13421342
kvec = A.diag
13431343
# For diag elem < 1e-6 we keep 1e-6.
1344-
kvec = np.where(kvec > 1.e-6, kvec, 1.e-6)
1344+
kvec = np.maximum(kvec, 1e-6)
13451345

13461346
# Initial guess
13471347
if x0 is None:

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.