@@ -5237,7 +5237,7 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
5237
5237
Elements of RGB and RGBA arrays represent pixels of an MxN image.
5238
5238
All values - unless normalised with `norm` or `vmin` and `vmax` -
5239
5239
should be in the range [0 .. 1] for floats or [0 .. 255] for
5240
- integers.
5240
+ integers. Out-of-range values will be clipped to these bounds.
5241
5241
5242
5242
cmap : `~matplotlib.colors.Colormap`, optional, default: None
5243
5243
If None, default to rc `image.cmap` value. `cmap` is ignored
@@ -5362,6 +5362,20 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
5362
5362
# Disable later styling based on these values
5363
5363
norm , vmin , vmax = None , None , None
5364
5364
5365
+ # If the input data has values outside the valid range (after
5366
+ # normalisation), we issue a warning and then clip X to the bounds
5367
+ # - otherwise casting wraps extreme values, hiding outliers and
5368
+ # making reliable interpretation impossible.
5369
+ high_bound = 255 if np .issubdtype (X .dtype , np .integer ) else 1
5370
+ if X .min () < 0 or high_bound < X .max ():
5371
+ warnings .warn (
5372
+ 'Clipping input data to the valid range for imshow with '
5373
+ 'RGB data ([0..1] for floats or [0..255] for integers). '
5374
+ 'Pass the `norm`, `vmin`, or `vmax` arguments to avoid '
5375
+ 'this error.'
5376
+ )
5377
+ X = np .clip (X , 0 , high_bound )
5378
+
5365
5379
im = mimage .AxesImage (self , cmap , norm , interpolation , origin , extent ,
5366
5380
filternorm = filternorm , filterrad = filterrad ,
5367
5381
resample = resample , ** kwargs )
0 commit comments