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

Skip to content

Commit f0950ac

Browse files
committed
Allow normalising RGB images
1 parent 2facc69 commit f0950ac

3 files changed

Lines changed: 41 additions & 13 deletions

File tree

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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
`Axes.imshow` can normalise RGB images
2+
--------------------------------------
3+
4+
`Axes.imshow` can now apply the ``norm`` or ``vmin`` and ``vmax`` arguments
5+
to RGB images, allowing easy display of high-bit-depth data, or adjustments
6+
of brightness and contrast.

lib/matplotlib/axes/_axes.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5222,18 +5222,22 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
52225222
Parameters
52235223
----------
52245224
X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)
5225-
Display the image in `X` to current axes. `X` may be an
5226-
array or a PIL image. If `X` is an array, it
5227-
can have the following shapes and types:
5225+
Display the image in `X` to current axes. `X` may be an array
5226+
or a PIL image. If `X` is an array, it may have the following
5227+
shapes, and either floating or integer dtypes:
52285228
5229-
- MxN -- values to be mapped (float or int)
5230-
- MxNx3 -- RGB (float or uint8)
5231-
- MxNx4 -- RGBA (float or uint8)
5229+
- MxN -- values to be mapped
5230+
- MxNx3 -- RGB
5231+
- MxNx4 -- RGBA
52325232
5233-
The value for each component of MxNx3 and MxNx4 float arrays
5234-
should be in the range 0.0 to 1.0. MxN arrays are mapped
5235-
to colors based on the `norm` (mapping scalar to scalar)
5236-
and the `cmap` (mapping the normed scalar to a color).
5233+
MxN arrays are mapped to colors based on the `norm` (mapping
5234+
scalar to scalar) and the `cmap` (mapping the normed scalar to
5235+
a color).
5236+
5237+
Elements of RGB and RGBA arrays represent pixels of an MxN image.
5238+
All values - unless normalised with `norm` or `vmin` and `vmax` -
5239+
should be in the range [0 .. 1] for floats or [0 .. 255] for
5240+
integers.
52375241
52385242
cmap : `~matplotlib.colors.Colormap`, optional, default: None
52395243
If None, default to rc `image.cmap` value. `cmap` is ignored
@@ -5271,11 +5275,13 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
52715275
52725276
vmin, vmax : scalar, optional, default: None
52735277
`vmin` and `vmax` are used in conjunction with norm to normalize
5274-
luminance data. Note if you pass a `norm` instance, your
5275-
settings for `vmin` and `vmax` will be ignored.
5278+
luminance, RGB, or RGBA data. Note if you pass a `norm` instance,
5279+
your settings for `vmin` and `vmax` will be ignored.
52765280
52775281
alpha : scalar, optional, default: None
5278-
The alpha blending value, between 0 (transparent) and 1 (opaque)
5282+
The alpha blending value, between 0 (transparent) and 1 (opaque).
5283+
For RGBA images, this is applied in addition to the data's alpha
5284+
channel.
52795285
52805286
origin : ['upper' | 'lower'], optional, default: None
52815287
Place the [0,0] index of the array in the upper left or lower left
@@ -5341,6 +5347,21 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
53415347
if aspect is None:
53425348
aspect = rcParams['image.aspect']
53435349
self.set_aspect(aspect)
5350+
5351+
if X.ndim == 3:
5352+
# Much of the standard machinery doesn't work implicitly for RGB
5353+
# and RGBA images, so we replicate the effects here.
5354+
5355+
# RGB and RGBA arrays can't be implicitly normalised, but explicit
5356+
# normalisation works just fine! However, we only normalise if
5357+
# an argument is given - otherwise you can't display a dark image.
5358+
if norm is None and (vmin is not None or vmax is not None):
5359+
norm = mcolors.Normalize(vmin, vmax, clip=True)
5360+
if norm is not None:
5361+
X = norm(X)
5362+
# Disable later styling based on these values
5363+
norm, vmin, vmax = None, None, None
5364+
53445365
im = mimage.AxesImage(self, cmap, norm, interpolation, origin, extent,
53455366
filternorm=filternorm, filterrad=filterrad,
53465367
resample=resample, **kwargs)

0 commit comments

Comments
 (0)