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

Skip to content

Commit 8a985f0

Browse files
committed
Defer call to norm and cmap in imshow
1 parent f9dc4b1 commit 8a985f0

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5137,11 +5137,6 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
51375137
of pixel (0, 0).
51385138
51395139
"""
5140-
5141-
temp = np.asarray(X)
5142-
if temp.ndim == 3 and isinstance(norm, mcolors.BivariateNorm):
5143-
X = norm(temp)
5144-
51455140
if not self._hold:
51465141
self.cla()
51475142

lib/matplotlib/axes/_subplots.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ def subplot_class_factory(axes_class=None):
177177
(SubplotBase, axes_class),
178178
{'_axes_class': axes_class})
179179
_subplot_classes[axes_class] = new_class
180-
181180
return new_class
182181

183182
# This is provided for backward compatibility

lib/matplotlib/image.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,11 @@ def get_size(self):
253253
if self._A is None:
254254
raise RuntimeError('You must first set the image array')
255255

256-
return self._A.shape[:2]
256+
if isinstance(self.norm, mcolors.BivariateNorm):
257+
imshape = self._A.shape[1:]
258+
else:
259+
imshape = self._A.shape[:2]
260+
return imshape
257261

258262
def set_alpha(self, alpha):
259263
"""
@@ -300,6 +304,12 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
300304
`trans` is the affine transformation from the image to pixel
301305
space.
302306
"""
307+
if isinstance(self.norm, mcolors.BivariateNorm):
308+
imwidth = A.shape[1]
309+
imheight = A.shape[2]
310+
else:
311+
imwidth = A.shape[0]
312+
imheight = A.shape[1]
303313
if A is None:
304314
raise RuntimeError('You must first set the image '
305315
'array or the image attribute')
@@ -323,15 +333,15 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
323333
# Flip the input image using a transform. This avoids the
324334
# problem with flipping the array, which results in a copy
325335
# when it is converted to contiguous in the C wrapper
326-
t0 = Affine2D().translate(0, -A.shape[0]).scale(1, -1)
336+
t0 = Affine2D().translate(0, -imwidth).scale(1, -1)
327337
else:
328338
t0 = IdentityTransform()
329339

330340
t0 += (
331341
Affine2D()
332342
.scale(
333-
in_bbox.width / A.shape[1],
334-
in_bbox.height / A.shape[0])
343+
in_bbox.width / imheight,
344+
in_bbox.height / imwidth)
335345
.translate(in_bbox.x0, in_bbox.y0)
336346
+ self.get_transform())
337347

@@ -362,17 +372,17 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
362372
if A.ndim not in (2, 3):
363373
raise ValueError("Invalid dimensions, got {}".format(A.shape))
364374

365-
if A.ndim == 2:
366-
if not isinstance(self.norm, mcolors.BivariateNorm):
367-
A = self.norm(A)
375+
if A.ndim == 2 or (A.ndim == 3 and
376+
isinstance(self.norm, mcolors.BivariateNorm)):
377+
A = self.norm(A)
368378
if A.dtype.kind == 'f':
369379
# If the image is greyscale, convert to RGBA and
370380
# use the extra channels for resizing the over,
371381
# under, and bad pixels. This is needed because
372382
# Agg's resampler is very aggressive about
373383
# clipping to [0, 1] and we use out-of-bounds
374384
# values to carry the over/under/bad information
375-
rgba = np.empty((A.shape[0], A.shape[1], 4), dtype=A.dtype)
385+
rgba = np.empty((imwidth, imheight, 4), dtype=A.dtype)
376386
rgba[..., 0] = A # normalized data
377387
# this is to work around spurious warnings coming
378388
# out of masked arrays.
@@ -411,9 +421,10 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
411421

412422
if not created_rgba_mask:
413423
# Always convert to RGBA, even if only RGB input
424+
isBivari = (A.ndim == 2 and A.shape[0] == 2)
414425
if A.shape[2] == 3:
415426
A = _rgb_to_rgba(A)
416-
elif A.shape[2] != 4:
427+
elif A.shape[2] != 4 and not isBivari:
417428
raise ValueError("Invalid dimensions, got %s" % (A.shape,))
418429

419430
output = np.zeros((out_height, out_width, 4), dtype=A.dtype)
@@ -596,8 +607,9 @@ def set_data(self, A):
596607
not np.can_cast(self._A.dtype, float, "same_kind")):
597608
raise TypeError("Image data cannot be converted to float")
598609

599-
if not (self._A.ndim == 2
600-
or self._A.ndim == 3 and self._A.shape[-1] in [3, 4]):
610+
isRGB = (self._A.ndim == 3 and self._A.shape[-1] in [3, 4])
611+
isBivari = (self._A.ndim == 3 and self._A.shape[0] == 2)
612+
if not (self._A.ndim == 2 or isRGB or isBivari):
601613
raise TypeError("Invalid dimensions for image data")
602614

603615
self._imcache = None

0 commit comments

Comments
 (0)