Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1290594

Browse files
committed
Clip RGB data to vaild range in Axes.imshow
1 parentf6ebbc3 commit1290594

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

‎doc/users/credits.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ Yu Feng,
386386
Yunfei Yang,
387387
Yuri D'Elia,
388388
Yuval Langer,
389+
Zac Hatfield-Dodds,
389390
Zach Pincus,
390391
Zair Mubashar,
391392
alex,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
`Axes.imshow` clips RGB values to the valid range
2+
-------------------------------------------------
3+
4+
When `Axes.imshow` is passed an RGB or RGBA value with out-of-range
5+
values, it now issues a warning and clips them to the valid range.
6+
The old behaviour, wrapping back in to the range, often hid outliers
7+
and made interpreting RGB images unreliable.

‎lib/matplotlib/axes/_axes.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5228,10 +5228,14 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
52285228
- MxNx3 -- RGB (float or uint8)
52295229
- MxNx4 -- RGBA (float or uint8)
52305230
5231-
The value for each component of MxNx3 and MxNx4 float arrays
5232-
should be in the range 0.0 to 1.0. MxN arrays are mapped
5233-
to colors based on the `norm` (mapping scalar to scalar)
5234-
and the `cmap` (mapping the normed scalar to a color).
5231+
MxN arrays are mapped to colors based on the `norm` (mapping
5232+
scalar to scalar) and the `cmap` (mapping the normed scalar to
5233+
a color).
5234+
5235+
Elements of RGB and RGBA arrays represent pixels of an MxN image.
5236+
All values should be in the range [0 .. 1] for floats or
5237+
[0 .. 255] for integers. Out-of-range values will be clipped to
5238+
these bounds.
52355239
52365240
cmap : `~matplotlib.colors.Colormap`, optional, default: None
52375241
If None, default to rc `image.cmap` value. `cmap` is ignored
@@ -5273,7 +5277,8 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
52735277
settings for `vmin` and `vmax` will be ignored.
52745278
52755279
alpha : scalar, optional, default: None
5276-
The alpha blending value, between 0 (transparent) and 1 (opaque)
5280+
The alpha blending value, between 0 (transparent) and 1 (opaque).
5281+
The ``alpha`` argument is ignored for RGBA input data.
52775282
52785283
origin : ['upper' | 'lower'], optional, default: None
52795284
Place the [0,0] index of the array in the upper left or lower left

‎lib/matplotlib/cm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
259259
xx= (xx*255).astype(np.uint8)
260260
elifxx.dtype==np.uint8:
261261
ifnotbytes:
262-
xx=xx.astype(float)/255
262+
xx=xx.astype(np.float32)/255
263263
else:
264264
raiseValueError("Image RGB array must be uint8 or "
265265
"floating point; found %s"%xx.dtype)

‎lib/matplotlib/image.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
frommathimportceil
1515
importos
16+
importwarnings
1617

1718
importnumpyasnp
1819

@@ -610,6 +611,23 @@ def set_data(self, A):
610611
orself._A.ndim==3andself._A.shape[-1]in [3,4]):
611612
raiseTypeError("Invalid dimensions for image data")
612613

614+
ifself._A.ndim==3:
615+
# If the input data has values outside the valid range (after
616+
# normalisation), we issue a warning and then clip X to the bounds
617+
# - otherwise casting wraps extreme values, hiding outliers and
618+
# making reliable interpretation impossible.
619+
high=255ifnp.issubdtype(self._A.dtype,np.integer)else1
620+
ifself._A.min()<0orhigh<self._A.max():
621+
warnings.warn(
622+
'Clipping input data to the valid range for imshow with '
623+
'RGB data ([0..1] for floats or [0..255] for integers).'
624+
)
625+
self._A=np.clip(self._A,0,high)
626+
# Cast unsupported integer types to uint8
627+
ifself._A.dtype!=np.uint8andnp.issubdtype(self._A.dtype,
628+
np.integer):
629+
self._A=self._A.astype(np.uint8)
630+
613631
self._imcache=None
614632
self._rgbacache=None
615633
self.stale=True

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp