8000 Merge pull request #14321 from dstansby/auto-backport-of-pr-14313-on-… · matplotlib/matplotlib@ba4b58e · GitHub
[go: up one dir, main page]

Skip to content

Commit ba4b58e

Browse files
authored
Merge pull request #14321 from dstansby/auto-backport-of-pr-14313-on-v3.1.x
Backport PR #14313 on branch v3.1.x
2 parents 6036642 + eaa5576 commit ba4b58e

File tree

2 files changed

+32
-1
lines ch 8000 anged

2 files changed

+32
-1
lines changed

lib/matplotlib/colors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def to_rgba(c, alpha=None):
152152
153153
Parameters
154154
----------
155-
c : Matplotlib color
155+
c : Matplotlib color or ``np.ma.masked``
156156
157157
alpha : scalar, optional
158158
If *alpha* is not ``None``, it forces the alpha value, except if *c* is
@@ -189,6 +189,8 @@ def _to_rgba_no_colorcycle(c, alpha=None):
189189
``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``.
190190
"""
191191
orig_c = c
192+
if c is np.ma.masked:
193+
return (0., 0., 0., 0.)
192194
if isinstance(c, str):
193195
if c.lower() == "none":
194196
return (0., 0., 0., 0.)
@@ -254,19 +256,25 @@ def to_rgba_array(c, alpha=None):
254256
255257
If *alpha* is not ``None``, it forces the alpha value. If *c* is
256258
``"none"`` (case-insensitive) or an empty list, an empty array is returned.
259+
If *c* is a masked array, an ndarray is returned with a (0, 0, 0, 0)
260+
row for each masked value or row in *c*.
257261
"""
258262
# Special-case inputs that are already arrays, for performance. (If the
259263
# array has the wrong kind or shape, raise the error during one-at-a-time
260264
# conversion.)
261265
if (isinstance(c, np.ndarray) and c.dtype.kind in "if"
262266
and c.ndim == 2 and c.shape[1] in [3, 4]):
267+
mask = c.mask.any(axis=1) if np.ma.is_masked(c) else None
268+
c = np.ma.getdata(c)
263269
if c.shape[1] == 3:
264270
result = np.column_stack([c, np.zeros(len(c))])
265271
result[:, -1] = alpha if alpha is not None else 1.
266272
elif c.shape[1] == 4:
267273
result = c.copy()
268274
if alpha is not None:
269275
result[:, -1] = alpha
276+
if mask is not None:
277+
result[mask] = 0
270278
if np.any((result < 0) | (result > 1)):
271279
raise ValueError("RGBA values should be within 0-1 range")
272280
return result

lib/matplotlib/tests/test_colors.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,29 @@ def test_conversions():
766766
hex_color
767767

768768

769+
def test_conversions_masked():
770+
x1 = np.ma.array(['k', 'b'], mask=[True, False])
771+
x2 = np.ma.array([[0, 0, 0, 1], [0, 0, 1, 1]])
772+
x2[0] = np.ma.masked
773+
assert mcolors.to_rgba(x1[0]) == (0, 0, 0, 0)
774+
assert_array_equal(mcolors.to_rgba_array(x1),
775+
[[0, 0, 0, 0], [0, 0, 1, 1]])
776+
assert_array_equal(mcolors.to_rgba_array(x2), mcolors.to_rgba_array(x1))
777+
778+
779+
def test_to_rgba_array_single_str():
780+
# single color name is valid
781+
assert_array_equal(mcolors.to_rgba_array("red"), [(1, 0, 0, 1)])
782+
783+
# single char color sequence is deprecated
784+
array = mcolors.to_rgba_array("rgb")
785+
assert_array_equal(array, [(1, 0, 0, 1), (0, 0.5, 0, 1), (0, 0, 1, 1)])
786+
787+
with pytest.raises(ValueError,
788+
match="Invalid RGBA argument: 'x'"):
789+
mcolors.to_rgba_array("rgbx")
790+
791+
769792
def test_grey_gray():
770793
color_mapping = mcolors._colors_full_map
771794
for k in color_mapping.keys():

0 commit comments

Comments
 (0)
0