Skip to content

Fix imshow to support array alpha values and add corresponding test #30407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,10 @@
**kwargs
)

#support array value in imshow
if isinstance(self._alpha, np.ndarray):
self._set_alpha_for_array(self._alpha)

def get_window_extent(self, renderer=None):
x0, x1, y0, y1 = self._extent
bbox = Bbox.from_extents([x0, y0, x1, y1])
Expand Down Expand Up @@ -1579,8 +1583,10 @@

.. note::

If you want to save a single channel image as gray scale please use an
image I/O library (such as pillow, tifffile, or imageio) directly.
If *arr* is a single-channel (MxN) image and you want to save it as a grayscale image

Check warning on line 1586 in lib/matplotlib/image.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/image.py" range:{start:{line:1586 column:89} end:{line:1586 column:90}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:1586 column:89} end:{line:1586 column:90}}}

Check warning on line 1586 in lib/matplotlib/image.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Line too long (89 > 88) Raw Output: message:"Line too long (89 > 88)" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/image.py" range:{start:{line:1586 column:89} end:{line:1586 column:90}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E501" url:"https://docs.astral.sh/ruff/rules/line-too-long"}

Check warning on line 1586 in lib/matplotlib/image.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Docstring is under-indented Raw Output: message:"Docstring is under-indented" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/image.py" range:{start:{line:1586 column:1} end:{line:1586 column:1}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"D207" url:"https://docs.astral.sh/ruff/rules/under-indentation"} suggestions:{range:{start:{line:1586 column:1} end:{line:1586 column:4}} text:" "}
(instead of applying a colormap), consider using a dedicated image I/O library like

Check warning on line 1587 in lib/matplotlib/image.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Trailing whitespace Raw Output: message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/image.py" range:{start:{line:1587 column:87} end:{line:1587 column:88}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:1587 column:87} end:{line:1587 column:88}}}

Check warning on line 1587 in lib/matplotlib/image.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Docstring is under-indented Raw Output: message:"Docstring is under-indented" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/image.py" range:{start:{line:1587 column:1} end:{line:1587 column:1}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"D207" url:"https://docs.astral.sh/ruff/rules/under-indentation"} suggestions:{range:{start:{line:1587 column:1} end:{line:1587 column:4}} text:" "}
Pillow, imageio, or tifffile. `imsave` will apply a colormap by default.

Check warning on line 1588 in lib/matplotlib/image.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Docstring is under-indented Raw Output: message:"Docstring is under-indented" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/image.py" range:{start:{line:1588 column:1} end:{line:1588 column:1}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"D207" url:"https://docs.astral.sh/ruff/rules/under-indentation"} suggestions:{range:{start:{line:1588 column:1} end:{line:1588 column:4}} text:" "}


Parameters
----------
Expand Down Expand Up @@ -1658,6 +1664,7 @@
else:
sm = mcolorizer.Colorizer(cmap=cmap)
sm.set_clim(vmin, vmax)

rgba = sm.to_rgba(arr, bytes=True)
if pil_kwargs is None:
pil_kwargs = {}
Expand Down
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_axes/test_imshow_alpha_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import matplotlib.pyplot as plt
import numpy as np

def test_imshow_alpha_array():

Check warning on line 4 in lib/matplotlib/tests/test_axes/test_imshow_alpha_array.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Expected 2 blank lines, found 1 Raw Output: message:"Expected 2 blank lines, found 1" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_axes/test_imshow_alpha_array.py" range:{start:{line:4 column:1} end:{line:4 column:4}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E302" url:"https://docs.astral.sh/ruff/rules/blank-lines-top-level"} suggestions:{range:{start:{line:3 column:1} end:{line:4 column:1}} text:"\n\n"}
data = np.random.rand(10, 10)
alpha = np.linspace(0, 1, 100).reshape(10, 10)

fig, ax = plt.subplots()
im = ax.imshow(data, alpha=alpha)
plt.colorbar(im, ax=ax)
return fig
Loading