Skip to content

Commit 4544674

Browse files
committed
MNT: Fix handling of ints in rgb_to_hsv()
This is a numpy 2.0 regression.
1 parent 89aa371 commit 4544674

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

lib/matplotlib/colors.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3631,11 +3631,10 @@ def rgb_to_hsv(arr):
36313631
f"shape {arr.shape} was found.")
36323632

36333633
in_shape = arr.shape
3634-
arr = np.array(
3635-
arr, copy=False,
3636-
dtype=np.promote_types(arr.dtype, np.float32), # Don't work on ints.
3637-
ndmin=2, # In case input was 1D.
3638-
)
3634+
# ensure numerics are done at least on float32; ints are cast as well
3635+
arr = np.asarray(arr, dtype=np.promote_types(arr.dtype, np.float32))
3636+
if arr.ndim == 1:
3637+
arr = np.expand_dims(arr, axis=0) # ensure arr is 2D
36393638

36403639
out = np.zeros_like(arr)
36413640
arr_max = arr.max(-1)

lib/matplotlib/tests/test_colors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,11 @@ def test_rgb_hsv_round_trip():
947947
tt, mcolors.rgb_to_hsv(mcolors.hsv_to_rgb(tt)))
948948

949949

950+
def test_rgb_to_hsv_int():
951+
# Test that int rgb values (still range 0-1) are processed correctly.
952+
assert_array_equal(mcolors.rgb_to_hsv((0, 1, 0)), (1/3, 1, 1)) # green
953+
954+
950955
def test_autoscale_masked():
951956
# Test for #2336. Previously fully masked data would trigger a ValueError.
952957
data = np.ma.masked_all((12, 20))

0 commit comments

Comments
 (0)