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

Skip to content

Commit 1508bf1

Browse files
committed
Defer call to norm and cmap in imshow
1 parent a5cd938 commit 1508bf1

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
@@ -5119,11 +5119,6 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
51195119
of pixel (0, 0).
51205120
51215121
"""
5122-
5123-
temp = np.asarray(X)
5124-
if temp.ndim == 3 and isinstance(norm, mcolors.BivariateNorm):
5125-
X = norm(temp)
5126-
51275122
if not self._hold:
51285123
self.cla()
51295124

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
@@ -251,7 +251,11 @@ def get_size(self):
251251
if self._A is None:
252252
raise RuntimeError('You must first set the image array')
253253

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

256260
def set_alpha(self, alpha):
257261
"""
@@ -298,6 +302,12 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
298302
`trans` is the affine transformation from the image to pixel
299303
space.
300304
"""
305+
if isinstance(self.norm, mcolors.BivariateNorm):
306+
imwidth = A.shape[1]
307+
imheight = A.shape[2]
308+
else:
309+
imwidth = A.shape[0]
310+
imheight = A.shape[1]
301311
if A is None:
302312
raise RuntimeError('You must first set the image '
303313
'array or the image attribute')
@@ -321,15 +331,15 @@ def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
321331
# Flip the input image using a transform. This avoids the
322332
# problem with flipping the array, which results in a copy
323333
# when it is converted to contiguous in the C wrapper
324-
t0 = Affine2D().translate(0, -A.shape[0]).scale(1, -1)
334+
t0 = Affine2D().translate(0, -imwidth).scale(1, -1)
325335
else:
326336
t0 = IdentityTransform()
327337

328338
t0 += (
329339
Affine2D()
330340
.scale(
331-
in_bbox.width / A.shape[1],
332-
in_bbox.height / A.shape[0])
341+
in_bbox.width / imheight,
342+
in_bbox.height / imwidth)
333343
.translate(in_bbox.x0, in_bbox.y0)
334344
+ self.get_transform())
335345

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

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

411421
if not created_rgba_mask:
412422
# Always convert to RGBA, even if only RGB input
423+
isBivari = (A.ndim == 2 and A.shape[0] == 2)
413424
if A.shape[2] == 3:
414425
A = _rgb_to_rgba(A)
415-
elif A.shape[2] != 4:
426+
elif A.shape[2] != 4 and not isBivari:
416427
raise ValueError("Invalid dimensions, got %s" % (A.shape,))
417428

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

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

602614
self._imcache = None

0 commit comments

Comments
 (0)