From 28eab9590008c2c3ce28e4bfeff5ffc4806d645d Mon Sep 17 00:00:00 2001 From: AsadullahMaliyawala <117357627+AsadullahMaliyawala@users.noreply.github.com> Date: Mon, 30 Nov 2026 22:29:12 +0500 Subject: [PATCH 1/2] Fix px.imshow integer dtype detection for int32/uint32 on Windows With NumPy 2 on Windows, int32 and uint32 arrays have np.long/np.ulong as their scalar type, which is not one of the named aliases listed in _integer_types (np.intc/np.uintc are distinct types there and np.int_ is int64). px.imshow then treated such images as float data, so contrast_rescaling="infer" picked a value-based zmax (e.g. 255) instead of the dtype maximum. Build the type table from the dtype characters so every platform integer type is covered. Co-authored-by: Claude --- CHANGELOG.md | 3 +++ plotly/express/imshow_utils.py | 19 +++++++------------ tests/test_optional/test_px/test_imshow.py | 12 ++++++++++++ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dba830298e..d8662779dc7 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 [[#PRNUM](https://github.com/plotly/plotly.py/pull/PRNUM)] + ## [7.2.0] - 2026-11-24 diff --git a/plotly/express/imshow_utils.py b/plotly/express/imshow_utils.py index 7f110ede4d0..3a3c4e65eec 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 3a0e230eb9c..6d08feb2baf 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 From 281df93f9ff6d2bc2588b4295eb303494dfc7d17 Mon Sep 17 00:00:00 2001 From: AsadullahMaliyawala <117357627+AsadullahMaliyawala@users.noreply.github.com> Date: Mon, 30 Nov 2026 22:30:46 +0500 Subject: [PATCH 2/2] Add PR number to changelog entry Co-authored-by: Claude --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8662779dc7..63fc04abf4d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ 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 [[#PRNUM](https://github.com/plotly/plotly.py/pull/PRNUM)] +- 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