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

Skip to content

Commit d3cd57a

Browse files
committed
Replace np.compress by boolean indexing.
It's shorter, better known, and works directly on 2D inputs without fiddling with ravel()ing (for the changes for pcolor).
1 parent 2d05981 commit d3cd57a

6 files changed

Lines changed: 19 additions & 21 deletions

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5814,23 +5814,21 @@ def pcolor(self, *args, alpha=None, norm=None, cmap=None, vmin=None,
58145814
# don't plot if C or any of the surrounding vertices are masked.
58155815
mask = ma.getmaskarray(C) + xymask
58165816

5817-
compress = np.compress
5818-
5819-
ravelmask = (mask == 0).ravel()
5820-
X1 = compress(ravelmask, ma.filled(X[:-1, :-1]).ravel())
5821-
Y1 = compress(ravelmask, ma.filled(Y[:-1, :-1]).ravel())
5822-
X2 = compress(ravelmask, ma.filled(X[1:, :-1]).ravel())
5823-
Y2 = compress(ravelmask, ma.filled(Y[1:, :-1]).ravel())
5824-
X3 = compress(ravelmask, ma.filled(X[1:, 1:]).ravel())
5825-
Y3 = compress(ravelmask, ma.filled(Y[1:, 1:]).ravel())
5826-
X4 = compress(ravelmask, ma.filled(X[:-1, 1:]).ravel())
5827-
Y4 = compress(ravelmask, ma.filled(Y[:-1, 1:]).ravel())
5817+
unmask = ~mask
5818+
X1 = ma.filled(X[:-1, :-1])[unmask]
5819+
Y1 = ma.filled(Y[:-1, :-1])[unmask]
5820+
X2 = ma.filled(X[1:, :-1])[unmask]
5821+
Y2 = ma.filled(Y[1:, :-1])[unmask]
5822+
X3 = ma.filled(X[1:, 1:])[unmask]
5823+
Y3 = ma.filled(Y[1:, 1:])[unmask]
5824+
X4 = ma.filled(X[:-1, 1:])[unmask]
5825+
Y4 = ma.filled(Y[:-1, 1:])[unmask]
58285826
npoly = len(X1)
58295827

58305828
xy = np.stack([X1, Y1, X2, Y2, X3, Y3, X4, Y4, X1, Y1], axis=-1)
58315829
verts = xy.reshape((npoly, 5, 2))
58325830

5833-
C = compress(ravelmask, ma.filled(C[0:Ny - 1, 0:Nx - 1]).ravel())
5831+
C = ma.filled(C[:Ny - 1, :Nx - 1])[unmask]
58345832

58355833
linewidths = (0.25,)
58365834
if 'linewidth' in kwargs:

lib/matplotlib/cbook/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,23 +1311,23 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
13111311
hival = np.percentile(x, whis[1])
13121312

13131313
# get high extreme
1314-
wiskhi = np.compress(x <= hival, x)
1314+
wiskhi = x[x <= hival]
13151315
if len(wiskhi) == 0 or np.max(wiskhi) < q3:
13161316
stats['whishi'] = q3
13171317
else:
13181318
stats['whishi'] = np.max(wiskhi)
13191319

13201320
# get low extreme
1321-
wisklo = np.compress(x >= loval, x)
1321+
wisklo = x[x >= loval]
13221322
if len(wisklo) == 0 or np.min(wisklo) > q1:
13231323
stats['whislo'] = q1
13241324
else:
13251325
stats['whislo'] = np.min(wisklo)
13261326

13271327
# compute a single array of outliers
13281328
stats['fliers'] = np.hstack([
1329-
np.compress(x < stats['whislo'], x),
1330-
np.compress(x > stats['whishi'], x)
1329+
x[x < stats['whislo']],
1330+
x[x > stats['whishi']],
13311331
])
13321332

13331333
# add in the remaining stats

lib/matplotlib/ticker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2641,9 +2641,9 @@ def __call__(self):
26412641
mod = np.abs((locs - t0) % majorstep)
26422642
cond1 = mod > minorstep / 10.0
26432643
cond2 = ~np.isclose(mod, majorstep, atol=0)
2644-
locs = locs.compress(cond1 & cond2)
2644+
locs = locs[cond1 & cond2]
26452645

2646-
return self.raise_if_exceeds(np.array(locs))
2646+
return self.raise_if_exceeds(locs)
26472647

26482648
def tick_values(self, vmin, vmax):
26492649
raise NotImplementedError('Cannot get tick locations for a '

lib/matplotlib/tri/triangulation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def get_masked_triangles(self):
117117
Return an array of triangles that are not masked.
118118
"""
119119
if self.mask is not None:
120-
return self.triangles.compress(1 - self.mask, axis=0)
120+
return self.triangles[~self.mask]
121121
else:
122122
return self.triangles
123123

lib/matplotlib/tri/tripcolor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def tripcolor(ax, *args, alpha=1.0, norm=None, cmap=None, vmin=None,
112112
C = C[maskedTris].mean(axis=1)
113113
elif tri.mask is not None:
114114
# Remove color values of masked triangles.
115-
C = C.compress(1-tri.mask)
115+
C = C[~tri.mask]
116116

117117
collection = PolyCollection(verts, **kwargs)
118118

lib/matplotlib/tri/tritools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def _total_to_compress_renum(mask, n=None):
293293
n = np.size(mask)
294294
if mask is not None:
295295
renum = np.full(n, -1, dtype=np.int32) # Default num is -1
296-
valid = np.arange(n, dtype=np.int32).compress(~mask, axis=0)
296+
valid = np.arange(n, dtype=np.int32)[~mask]
297297
renum[valid] = np.arange(np.size(valid, 0), dtype=np.int32)
298298
return renum
299299
else:

0 commit comments

Comments
 (0)