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

Skip to content

Commit 4e43c8b

Browse files
committed
Use X.ndim over len(X.shape)
1 parent b894683 commit 4e43c8b

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/matplotlib/image.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,21 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
306306
out_height = int(out_height_base)
307307

308308
if not unsampled:
309-
if len(A.shape) == 2:
309+
if A.ndim == 2:
310310
A = self.norm(A)
311311
if A.dtype.kind == 'f':
312312
# For floating-point greyscale images, we treat negative
313313
# numbers as transparent.
314+
315+
# TODO: Use np.full when we support Numpy 1.9 as a
316+
# minimum
314317
output = np.empty((out_height, out_width), dtype=A.dtype)
315318
output[...] = -100.0
316319
else:
317320
output = np.zeros((out_height, out_width), dtype=A.dtype)
318321

319322
alpha = 1.0
320-
elif len(A.shape) == 3:
323+
elif A.ndim == 3:
321324
# Always convert to RGBA, even if only RGB input
322325
if A.shape[2] == 3:
323326
A = _rgb_to_rgba(A)
@@ -340,7 +343,7 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
340343
output = self.to_rgba(output, bytes=True, norm=False)
341344

342345
# Apply alpha *after* if the input was greyscale
343-
if len(A.shape) == 2:
346+
if A.ndim == 2:
344347
alpha = self.get_alpha()
345348
if alpha is not None and alpha != 1.0:
346349
alpha_channel = output[:, :, 3]
@@ -715,7 +718,7 @@ def make_image(self, renderer, magnification=1.0):
715718
raise RuntimeError('You must first set the image array')
716719

717720
A = self._A
718-
if len(A.shape) == 2:
721+
if A.ndim == 2:
719722
if A.dtype != np.uint8:
720723
A = self.to_rgba(A, bytes=True)
721724
self.is_grayscale = self.cmap.is_gray()
@@ -763,12 +766,12 @@ def set_data(self, x, y, A):
763766
if len(x.shape) != 1 or len(y.shape) != 1\
764767
or A.shape[0:2] != (y.shape[0], x.shape[0]):
765768
raise TypeError("Axes don't match array shape")
766-
if len(A.shape) not in [2, 3]:
769+
if A.ndim not in [2, 3]:
767770
raise TypeError("Can only plot 2D or 3D data")
768-
if len(A.shape) == 3 and A.shape[2] not in [1, 3, 4]:
771+
if A.ndim == 3 and A.shape[2] not in [1, 3, 4]:
769772
raise TypeError("3D arrays must have three (RGB) "
770773
"or four (RGBA) color components")
771-
if len(A.shape) == 3 and A.shape[2] == 1:
774+
if A.ndim == 3 and A.shape[2] == 1:
772775
A.shape = A.shape[0:2]
773776
self._A = A
774777
self._Ax = x

0 commit comments

Comments
 (0)