From dbf3eed3b614a3859d2126cb314f8eebcd5baaa8 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Fri, 24 May 2019 09:18:15 -1000 Subject: [PATCH 1/2] Support masked array inputs for to_rgba and to_rgba_array. Closes #14301. --- lib/matplotlib/colors.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 859f29b9054a..f90fb35a0d7f 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -152,7 +152,7 @@ def to_rgba(c, alpha=None): Parameters ---------- - c : Matplotlib color + c : Matplotlib color or ``np.ma.masked`` alpha : scalar, optional 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): ``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``. """ orig_c = c + if c is np.ma.masked: + return (0., 0., 0., 0.) if isinstance(c, str): if c.lower() == "none": return (0., 0., 0., 0.) @@ -260,12 +262,16 @@ def to_rgba_array(c, alpha=None): If *alpha* is not ``None``, it forces the alpha value. If *c* is ``"none"`` (case-insensitive) or an empty list, an empty array is returned. + If *c* is a masked array, an ndarray is returned with a (0, 0, 0, 0) + row for each masked value or row in *c*. """ # Special-case inputs that are already arrays, for performance. (If the # array has the wrong kind or shape, raise the error during one-at-a-time # conversion.) if (isinstance(c, np.ndarray) and c.dtype.kind in "if" and c.ndim == 2 and c.shape[1] in [3, 4]): + mask = c.mask.any(axis=1) if np.ma.is_masked(c) else None + c = np.ma.getdata(c) if c.shape[1] == 3: result = np.column_stack([c, np.zeros(len(c))]) result[:, -1] = alpha if alpha is not None else 1. @@ -273,6 +279,8 @@ def to_rgba_array(c, alpha=None): result = c.copy() if alpha is not None: result[:, -1] = alpha + if mask is not None: + result[mask] = 0 if np.any((result < 0) | (result > 1)): raise ValueError("RGBA values should be within 0-1 range") return result From 4d62e65be95ba5710e2b50872d4ba7c898a6bd43 Mon Sep 17 00:00:00 2001 From: Eric Firing Date: Fri, 24 May 2019 09:41:54 -1000 Subject: [PATCH 2/2] Add test --- lib/matplotlib/tests/test_colors.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 97b8a5e2e239..6721053f3e33 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -821,6 +821,16 @@ def test_conversions(): hex_color +def test_conversions_masked(): + x1 = np.ma.array(['k', 'b'], mask=[True, False]) + x2 = np.ma.array([[0, 0, 0, 1], [0, 0, 1, 1]]) + x2[0] = np.ma.masked + assert mcolors.to_rgba(x1[0]) == (0, 0, 0, 0) + assert_array_equal(mcolors.to_rgba_array(x1), + [[0, 0, 0, 0], [0, 0, 1, 1]]) + assert_array_equal(mcolors.to_rgba_array(x2), mcolors.to_rgba_array(x1)) + + def test_to_rgba_array_single_str(): # single color name is valid assert_array_equal(mcolors.to_rgba_array("red"), [(1, 0, 0, 1)])