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

Skip to content

Commit 4a0d9c5

Browse files
authored
Merge pull request #14313 from efiring/masked_to_rgba
Support masked array inputs for to_rgba and to_rgba_array.
2 parents 9ed01e9 + 4d62e65 commit 4a0d9c5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-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.)
@@ -260,19 +262,25 @@ def to_rgba_array(c, alpha=None):
260262
261263
If *alpha* is not ``None``, it forces the alpha value. If *c* is
262264
``"none"`` (case-insensitive) or an empty list, an empty array is returned.
265+
If *c* is a masked array, an ndarray is returned with a (0, 0, 0, 0)
266+
row for each masked value or row in *c*.
263267
"""
264268
# Special-case inputs that are already arrays, for performance. (If the
265269
# array has the wrong kind or shape, raise the error during one-at-a-time
266270
# conversion.)
267271
if (isinstance(c, np.ndarray) and c.dtype.kind in "if"
268272
and c.ndim == 2 and c.shape[1] in [3, 4]):
273+
mask = c.mask.any(axis=1) if np.ma.is_masked(c) else None
274+
c = np.ma.getdata(c)
269275
if c.shape[1] == 3:
270276
result = np.column_stack([c, np.zeros(len(c))])
271277
result[:, -1] = alpha if alpha is not None else 1.
272278
elif c.shape[1] == 4:
273279
result = c.copy()
274280
if alpha is not None:
275281
result[:, -1] = alpha
282+
if mask is not None:
283+
result[mask] = 0
276284
if np.any((result < 0) | (result > 1)):
277285
raise ValueError("RGBA values should be within 0-1 range")
278286
return result

lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,16 @@ def test_conversions():
821821
hex_color
822822

823823

824+
def test_conversions_masked():
825+
x1 = np.ma.array(['k', 'b'], mask=[True, False])
826+
x2 = np.ma.array([[0, 0, 0, 1], [0, 0, 1, 1]])
827+
x2[0] = np.ma.masked
828+
assert mcolors.to_rgba(x1[0]) == (0, 0, 0, 0)
829+
assert_array_equal(mcolors.to_rgba_array(x1),
830+
[[0, 0, 0, 0], [0, 0, 1, 1]])
831+
assert_array_equal(mcolors.to_rgba_array(x2), mcolors.to_rgba_array(x1))
832+
833+
824834
def test_to_rgba_array_single_str():
825835
# single color name is valid
826836
assert_array_equal(mcolors.to_rgba_array("red"), [(1, 0, 0, 1)])

0 commit comments

Comments
 (0)