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

Skip to content

Commit dbf3eed

Browse files
committed
Support masked array inputs for to_rgba and to_rgba_array.
Closes #14301.
1 parent 65e53ed commit dbf3eed

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-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

0 commit comments

Comments
 (0)