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

Skip to content

Commit c4fe048

Browse files
committed
Fix imshow to support array alpha values and add corresponding test
1 parent ed12550 commit c4fe048

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/matplotlib/image.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,10 @@ def __init__(self, ax,
903903
**kwargs
904904
)
905905

906+
#support array value in imshow
907+
if isinstance(self._alpha, np.ndarray):
908+
self._set_alpha_for_array(self._alpha)
909+
906910
def get_window_extent(self, renderer=None):
907911
x0, x1, y0, y1 = self._extent
908912
bbox = Bbox.from_extents([x0, y0, x1, y1])
@@ -1579,8 +1583,10 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15791583
15801584
.. note::
15811585
1582-
If you want to save a single channel image as gray scale please use an
1583-
image I/O library (such as pillow, tifffile, or imageio) directly.
1586+
If *arr* is a single-channel (MxN) image and you want to save it as a grayscale image
1587+
(instead of applying a colormap), consider using a dedicated image I/O library like
1588+
Pillow, imageio, or tifffile. `imsave` will apply a colormap by default.
1589+
15841590
15851591
Parameters
15861592
----------
@@ -1658,6 +1664,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
16581664
else:
16591665
sm = mcolorizer.Colorizer(cmap=cmap)
16601666
sm.set_clim(vmin, vmax)
1667+
16611668
rgba = sm.to_rgba(arr, bytes=True)
16621669
if pil_kwargs is None:
16631670
pil_kwargs = {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import matplotlib.pyplot as plt
2+
import numpy as np
3+
4+
def test_imshow_alpha_array():
5+
data = np.random.rand(10, 10)
6+
alpha = np.linspace(0, 1, 100).reshape(10, 10)
7+
8+
fig, ax = plt.subplots()
9+
im = ax.imshow(data, alpha=alpha)
10+
plt.colorbar(im, ax=ax)
11+
return fig

0 commit comments

Comments
 (0)