diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dba830298..63fc04abf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Fixed +- Fix `px.imshow` not recognising `int32`/`uint32` images as integer data on Windows with NumPy 2, which made `contrast_rescaling="infer"` use a value-based range (e.g. 255) instead of the dtype range [[#5917](https://github.com/plotly/plotly.py/pull/5917)] + ## [7.2.0] - 2026-11-24 diff --git a/plotly/express/imshow_utils.py b/plotly/express/imshow_utils.py index 7f110ede4d..3a3c4e65ee 100644 --- a/plotly/express/imshow_utils.py +++ b/plotly/express/imshow_utils.py @@ -6,18 +6,13 @@ from warnings import warn -_integer_types = ( - np.byte, - np.ubyte, # 8 bits - np.short, - np.ushort, # 16 bits - np.intc, - np.uintc, # 16 or 32 or 64 bits - np.int_, - np.uint, # 32 or 64 bits - np.longlong, - np.ulonglong, -) # 64 bits +# Build the integer scalar types from their dtype characters rather than +# from named aliases: on Windows with NumPy 2, int32/uint32 arrays have +# ``np.long``/``np.ulong`` as their scalar type, which is distinct from +# ``np.intc``/``np.uintc`` and is not covered by ``np.int_`` (int64 there). +_integer_types = tuple( + dict.fromkeys(np.dtype(char).type for char in "bBhHiIlLqQ") +) # 8, 16, 32 and 64 bits, signed and unsigned _integer_ranges = {t: (np.iinfo(t).min, np.iinfo(t).max) for t in _integer_types} dtype_range = { np.bool_: (False, True), diff --git a/tests/test_optional/test_px/test_imshow.py b/tests/test_optional/test_px/test_imshow.py index 3a0e230eb9..6d08feb2ba 100644 --- a/tests/test_optional/test_px/test_imshow.py +++ b/tests/test_optional/test_px/test_imshow.py @@ -553,3 +553,15 @@ def test_imshow_xarray_facet_row_and_col(): annotations = [a.text for a in fig.layout.annotations] assert any("row_dim=R1" in a for a in annotations) assert any("col_dim=C1" in a for a in annotations) + + +# uint8 ("B") is left out: with contrast_rescaling="infer" it keeps zmax=None. +@pytest.mark.parametrize("dtype_char", list("bhHiIlLqQ")) +def test_imshow_infer_zmax_all_integer_dtypes(dtype_char): + # Every NumPy integer dtype must be recognised as an integer type, including + # int32/uint32 on Windows with NumPy 2, where their scalar type is + # np.long/np.ulong rather than np.intc/np.uintc. + dtype = np.dtype(dtype_char) + img = np.arange(100, dtype=dtype).reshape((10, 10)) + fig = px.imshow(img, contrast_rescaling="infer") + assert fig.layout.coloraxis.cmax == np.iinfo(dtype).max