From 72bf994e82553bae4c2075c57db74f44c158c872 Mon Sep 17 00:00:00 2001 From: Ruth Comer <10599679+rcomer@users.noreply.github.com> Date: Thu, 28 Sep 2023 19:33:48 +0100 Subject: [PATCH] FIX 2-tuple of colors in to_rgba_array --- lib/matplotlib/colors.py | 2 +- lib/matplotlib/tests/test_colors.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 584adba8eace..b9866974d8a3 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -435,7 +435,7 @@ def to_rgba_array(c, alpha=None): (n, 4) array of RGBA colors, where each channel (red, green, blue, alpha) can assume values between 0 and 1. """ - if isinstance(c, tuple) and len(c) == 2: + if isinstance(c, tuple) and len(c) == 2 and isinstance(c[1], Real): if alpha is None: c, alpha = c else: diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 1c77f995fb53..139efbe17407 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -1298,6 +1298,11 @@ def test_to_rgba_array_single_str(): array = mcolors.to_rgba_array("rgb") +def test_to_rgba_array_2tuple_str(): + expected = np.array([[0, 0, 0, 1], [1, 1, 1, 1]]) + assert_array_equal(mcolors.to_rgba_array(("k", "w")), expected) + + def test_to_rgba_array_alpha_array(): with pytest.raises(ValueError, match="The number of colors must match"): mcolors.to_rgba_array(np.ones((5, 3), float), alpha=np.ones((2,)))