From 1b0374a3ec4ce9678fc46aebc9612a514e2bc910 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 21 Jul 2026 03:59:13 -0400 Subject: [PATCH] Backport PR #32058: MNT: Fix handling of ints in hsv_to_rgb() --- lib/matplotlib/colors.py | 9 ++++----- lib/matplotlib/tests/test_colors.py | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 010f73131fbc..fe80faab72e0 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -3729,11 +3729,10 @@ def hsv_to_rgb(hsv): f"shape {hsv.shape} was found.") in_shape = hsv.shape - hsv = np.array( - hsv, copy=False, - dtype=np.promote_types(hsv.dtype, np.float32), # Don't work on ints. - ndmin=2, # In case input was 1D. - ) + # ensure numerics are done at least on float32; ints are cast as well + hsv = np.asarray(hsv, dtype=np.promote_types(hsv.dtype, np.float32)) + if hsv.ndim == 1: + hsv = np.expand_dims(hsv, axis=0) # ensure hsv is 2D h = hsv[..., 0] s = hsv[..., 1] diff --git a/lib/matplotlib/tests/test_colors.py b/lib/matplotlib/tests/test_colors.py index 2ef9b7c5d091..32e155e47f72 100644 --- a/lib/matplotlib/tests/test_colors.py +++ b/lib/matplotlib/tests/test_colors.py @@ -946,6 +946,11 @@ def test_rgb_to_hsv_int(): assert_array_equal(mcolors.rgb_to_hsv((0, 1, 0)), (1/3, 1, 1)) # green +def test_hsv_to_rgb_int(): + # Test that int hsv values (still range 0-1) are processed correctly. + assert_array_equal(mcolors.hsv_to_rgb((0, 1, 1)), (1, 0, 0)) # red + + def test_autoscale_masked(): # Test for #2336. Previously fully masked data would trigger a ValueError. data = np.ma.masked_all((12, 20))