Skip to content

FIX: Handle masked arrays for RGBA input with ScalarMappables #26096

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

Merged
merged 1 commit into from
Jun 10, 2023
Merged
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
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/behavior/26096-GL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``ScalarMappable.to_rgba()`` now respects the mask of RGB(A) arrays
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously, the mask was ignored. Now the alpha channel is set to 0 if any
component (R, G, B, or A) is masked.
6 changes: 5 additions & 1 deletion lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
treated as an RGB or RGBA array, and no mapping will be done.
The array can be `~numpy.uint8`, or it can be floats with
values in the 0-1 range; otherwise a ValueError will be raised.
If it is a masked array, the mask will be ignored.
If it is a masked array, any masked elements will be set to 0 alpha.
If the last dimension is 3, the *alpha* kwarg (defaulting to 1)
will be used to fill in the transparency. If the last dimension
is 4, the *alpha* kwarg is ignored; it does not
Expand Down Expand Up @@ -493,6 +493,10 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
else:
raise ValueError("Image RGB array must be uint8 or "
"floating point; found %s" % xx.dtype)
# Account for any masked entries in the original array
# If any of R, G, B, or A are masked for an entry, we set alpha to 0
if np.ma.is_masked(x):
xx[np.any(np.ma.getmaskarray(x), axis=2), 3] = 0
return xx
except AttributeError:
# e.g., x is not an ndarray; so try mapping it
Expand Down
42 changes: 42 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,48 @@ def test_to_rgba_error_with_color_invalid_alpha_tuple():
mcolors.to_rgba(('blue', 2.0))


@pytest.mark.parametrize("bytes", (True, False))
def test_scalarmappable_to_rgba(bytes):
sm = cm.ScalarMappable()
alpha_1 = 255 if bytes else 1

# uint8 RGBA
x = np.ones((2, 3, 4), dtype=np.uint8)
expected = x.copy() if bytes else x.astype(np.float32)/255
np.testing.assert_array_equal(sm.to_rgba(x, bytes=bytes), expected)
# uint8 RGB
expected[..., 3] = alpha_1
np.testing.assert_array_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
# uint8 masked RGBA
xm = np.ma.masked_array(x, mask=np.zeros_like(x))
xm.mask[0, 0, 0] = True
expected = x.copy() if bytes else x.astype(np.float32)/255
expected[0, 0, 3] = 0
np.testing.assert_array_equal(sm.to_rgba(xm, bytes=bytes), expected)
# uint8 masked RGB
expected[..., 3] = alpha_1
expected[0, 0, 3] = 0
np.testing.assert_array_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)

# float RGBA
x = np.ones((2, 3, 4), dtype=float) * 0.5
expected = (x * 255).astype(np.uint8) if bytes else x.copy()
np.testing.assert_array_equal(sm.to_rgba(x, bytes=bytes), expected)
# float RGB
expected[..., 3] = alpha_1
np.testing.assert_array_equal(sm.to_rgba(x[..., :3], bytes=bytes), expected)
# float masked RGBA
xm = np.ma.masked_array(x, mask=np.zeros_like(x))
xm.mask[0, 0, 0] = True
expected = (x * 255).astype(np.uint8) if bytes else x.copy()
expected[0, 0, 3] = 0
np.testing.assert_array_equal(sm.to_rgba(xm, bytes=bytes), expected)
# float masked RGB
expected[..., 3] = alpha_1
expected[0, 0, 3] = 0
np.testing.assert_array_equal(sm.to_rgba(xm[..., :3], bytes=bytes), expected)


def test_failed_conversions():
with pytest.raises(ValueError):
mcolors.to_rgba('5')
Expand Down