diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 9a493c928fdb..c0f86ea75bc2 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -506,6 +506,11 @@ def to_rgba_array(c, alpha=None): if alpha is not None: rgba[:, 3] = alpha + if isinstance(c, Sequence): + # ensure that an explicit alpha does not overwrite full transparency + # for "none" + none_mask = [cbook._str_equal(cc, "none") for cc in c] + rgba[:, 3][none_mask] = 0 return rgba diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 2d71fea83472..b8af0c69b5aa 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -19,6 +19,7 @@ import matplotlib.scale as mscale from matplotlib.rcsetup import cycler from matplotlib.testing.decorators import image_comparison, check_figures_equal +from matplotlib.colors import to_rgba_array @pytest.mark.parametrize('N, result', [ @@ -1660,3 +1661,13 @@ def test_set_cmap_mismatched_name(): cmap_returned = plt.get_cmap("wrong-cmap") assert cmap_returned == cmap assert cmap_returned.name == "wrong-cmap" + + +def test_to_rgba_array_none_color_with_alpha_param(): + # effective alpha for color "none" must always be 0 to achieve a vanishing color + # even explicit alpha must be ignored + c = ["blue", "none"] + alpha = [1, 1] + assert_array_equal( + to_rgba_array(c, alpha), [[0., 0., 1., 1.], [0., 0., 0., 0.]] + )