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

Skip to content

Commit 61f739d

Browse files
committed
Use ndim, size whereever appropriate.
1 parent 984049e commit 61f739d

File tree

9 files changed

+19
-33
lines changed

9 files changed

+19
-33
lines changed

examples/statistics/errorbar_limits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
# Plot a series with lower and upper limits in both x & y
5353
# constant x-error with varying y-error
5454
xerr = 0.2
55-
yerr = np.zeros(x.shape) + 0.2
55+
yerr = np.zeros_like(x) + 0.2
5656
yerr[[3, 6]] = 0.3
5757

5858
# mock up some limits by modifying previous data

lib/matplotlib/axes/_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5214,10 +5214,10 @@ def _pcolorargs(funcname, *args, **kw):
52145214

52155215
Nx = X.shape[-1]
52165216
Ny = Y.shape[0]
5217-
if len(X.shape) != 2 or X.shape[0] == 1:
5217+
if X.ndim != 2 or X.shape[0] == 1:
52185218
x = X.reshape(1, Nx)
52195219
X = x.repeat(Ny, axis=0)
5220-
if len(Y.shape) != 2 or Y.shape[1] == 1:
5220+
if Y.ndim != 2 or Y.shape[1] == 1:
52215221
y = Y.reshape(Ny, 1)
52225222
Y = y.repeat(Nx, axis=1)
52235223
if X.shape != Y.shape:

lib/matplotlib/image.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,7 @@ def set_data(self, x, y, A):
848848
x = np.array(x, np.float32)
849849
y = np.array(y, np.float32)
850850
A = cbook.safe_masked_invalid(A, copy=True)
851-
if len(x.shape) != 1 or len(y.shape) != 1\
852-
or A.shape[0:2] != (y.shape[0], x.shape[0]):
851+
if not (x.ndim == y.ndim == 1 and A.shape[0:2] == y.shape + x.shape):
853852
raise TypeError("Axes don't match array shape")
854853
if A.ndim not in [2, 3]:
855854
raise TypeError("Can only plot 2D or 3D data")

lib/matplotlib/mlab.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def _single_spectrum_helper(x, mode, Fs=None, window=None, pad_to=None,
802802
if mode != 'complex':
803803
spec = spec.real
804804

805-
if len(spec.shape) == 2 and spec.shape[1] == 1:
805+
if spec.ndim == 2 and spec.shape[1] == 1:
806806
spec = spec[:, 0]
807807

808808
return spec, freqs
@@ -1013,7 +1013,7 @@ def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None,
10131013
sides=sides, scale_by_freq=scale_by_freq,
10141014
mode='psd')
10151015

1016-
if len(Pxy.shape) == 2:
1016+
if Pxy.ndim == 2:
10171017
if Pxy.shape[1] > 1:
10181018
Pxy = Pxy.mean(axis=1)
10191019
else:
@@ -1673,16 +1673,12 @@ def project(self, x, minfrac=0.):
16731673
of variance<minfrac
16741674
'''
16751675
x = np.asarray(x)
1676-
1677-
ndims = len(x.shape)
1678-
1679-
if (x.shape[-1] != self.numcols):
1676+
if x.shape[-1] != self.numcols:
16801677
raise ValueError('Expected an array with dims[-1]==%d' %
16811678
self.numcols)
1682-
16831679
Y = np.dot(self.Wt, self.center(x).T).T
16841680
mask = self.fracs >= minfrac
1685-
if ndims == 2:
1681+
if x.ndim == 2:
16861682
Yreduced = Y[:, mask]
16871683
else:
16881684
Yreduced = Y[mask]

lib/matplotlib/testing/compare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def calculate_rms(expectedImage, actualImage):
254254
raise ImageComparisonFailure(
255255
"image sizes do not match expected size: {0} "
256256
"actual size {1}".format(expectedImage.shape, actualImage.shape))
257-
num_values = np.prod(expectedImage.shape)
257+
num_values = expectedImage.size
258258
abs_diff_image = abs(expectedImage - actualImage)
259259
histogram = np.bincount(abs_diff_image.ravel(), minlength=256)
260260
sum_of_squares = np.sum(histogram * np.arange(len(histogram)) ** 2)

lib/matplotlib/tests/test_axes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,13 +2359,13 @@ def test_errorbar_limits():
23592359
plt.errorbar(x, y, xerr=xerr, yerr=yerr, ls=ls, color='blue')
23602360

23612361
# including upper limits
2362-
uplims = np.zeros(x.shape)
2362+
uplims = np.zeros_like(x)
23632363
uplims[[1, 5, 9]] = True
23642364
plt.errorbar(x, y+0.5, xerr=xerr, yerr=yerr, uplims=uplims, ls=ls,
23652365
color='green')
23662366

23672367
# including lower limits
2368-
lolims = np.zeros(x.shape)
2368+
lolims = np.zeros_like(x)
23692369
lolims[[2, 4, 8]] = True
23702370
plt.errorbar(x, y+1.0, xerr=xerr, yerr=yerr, lolims=lolims, ls=ls,
23712371
color='red')
@@ -2376,12 +2376,12 @@ def test_errorbar_limits():
23762376

23772377
# including xlower and xupper limits
23782378
xerr = 0.2
2379-
yerr = np.zeros(x.shape) + 0.2
2379+
yerr = np.zeros_like(x) + 0.2
23802380
yerr[[3, 6]] = 0.3
23812381
xlolims = lolims
23822382
xuplims = uplims
2383-
lolims = np.zeros(x.shape)
2384-
uplims = np.zeros(x.shape)
2383+
lolims = np.zeros_like(x)
2384+
uplims = np.zeros_like(x)
23852385
lolims[[6]] = True
23862386
uplims[[3]] = True
23872387
plt.errorbar(x, y+2.1, marker='o', ms=8, xerr=xerr, yerr=yerr,

lib/matplotlib/tri/triangulation.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Triangulation(object):
4141
def __init__(self, x, y, triangles=None, mask=None):
4242
self.x = np.asarray(x, dtype=np.float64)
4343
self.y = np.asarray(y, dtype=np.float64)
44-
if self.x.shape != self.y.shape or len(self.x.shape) != 1:
44+
if self.x.shape != self.y.shape or self.x.ndim != 1:
4545
raise ValueError("x and y must be equal-length 1-D arrays")
4646

4747
self.mask = None
@@ -67,8 +67,7 @@ def __init__(self, x, y, triangles=None, mask=None):
6767

6868
if mask is not None:
6969
self.mask = np.asarray(mask, dtype=np.bool)
70-
if (len(self.mask.shape) != 1 or
71-
self.mask.shape[0] != self.triangles.shape[0]):
70+
if self.mask.shape != (self.triangles.shape[0],):
7271
raise ValueError('mask array must have same length as '
7372
'triangles array')
7473

@@ -202,8 +201,7 @@ def set_mask(self, mask):
202201
self.mask = None
203202
else:
204203
self.mask = np.asarray(mask, dtype=np.bool)
205-
if (len(self.mask.shape) != 1 or
206-
self.mask.shape[0] != self.triangles.shape[0]):
204+
if self.mask.shape != (self.triangles.shape[0],):
207205
raise ValueError('mask array must have same length as '
208206
'triangles array')
209207

lib/matplotlib/tri/triinterpolate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def _interpolate_multikeys(self, x, y, tri_index=None,
166166
x = np.asarray(x, dtype=np.float64)
167167
y = np.asarray(y, dtype=np.float64)
168168
sh_ret = x.shape
169-
if (x.shape != y.shape):
169+
if x.shape != y.shape:
170170
raise ValueError("x and y shall have same shapes."
171171
" Given: {0} and {1}".format(x.shape, y.shape))
172172
x = np.ravel(x)

lib/mpl_toolkits/mplot3d/proj3d.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,7 @@ def inv_transform(xs, ys, zs, M):
152152
return vecr[0], vecr[1], vecr[2]
153153

154154
def vec_pad_ones(xs, ys, zs):
155-
try:
156-
try:
157-
vec = np.array([xs,ys,zs,np.ones(xs.shape)])
158-
except (AttributeError,TypeError):
159-
vec = np.array([xs,ys,zs,np.ones((len(xs)))])
160-
except TypeError:
161-
vec = np.array([xs,ys,zs,1])
162-
return vec
155+
return np.array([xs, ys, zs, np.ones_like(xs)])
163156

164157
def proj_transform(xs, ys, zs, M):
165158
"""

0 commit comments

Comments
 (0)