diff --git a/CHANGELOG.md b/CHANGELOG.md index 2dba830298..ccb183090e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Fixed +- Fix `px.imshow` ignoring `zmin`/`zmax` when only one of them is passed and the image contains NaN values, since the missing bound was computed as NaN [[#5778](https://github.com/plotly/plotly.py/pull/5778)], with thanks to @MohammadHijjawi97 for the contribution! ## [7.2.0] - 2026-11-24 diff --git a/plotly/express/_imshow.py b/plotly/express/_imshow.py index 576b114d51..ca4c721446 100644 --- a/plotly/express/_imshow.py +++ b/plotly/express/_imshow.py @@ -412,9 +412,9 @@ def imshow( if contrast_rescaling == "minmax": # When using binary_string and minmax we need to set zmin and zmax to rescale the image if (zmin is not None or binary_string) and zmax is None: - zmax = img.max() + zmax = np.nanmax(img) if (zmax is not None or binary_string) and zmin is None: - zmin = img.min() + zmin = np.nanmin(img) else: # For uint8 data and infer we let zmin and zmax to be None if passed as None if zmax is None and img.dtype != np.uint8: diff --git a/tests/test_optional/test_px/test_imshow.py b/tests/test_optional/test_px/test_imshow.py index 3a0e230eb9..dc7522be85 100644 --- a/tests/test_optional/test_px/test_imshow.py +++ b/tests/test_optional/test_px/test_imshow.py @@ -183,6 +183,17 @@ def test_zmin_zmax_range_color(): assert fig.layout.coloraxis.cmax == 0.8 +def test_zmin_zmax_nan_data(): + # NaN values should be ignored when inferring the missing bound + img = np.array([[1.0, 2.0], [3.0, np_nan()]]) + fig = px.imshow(img, zmax=2) + assert fig.layout.coloraxis.cmin == 1.0 + assert fig.layout.coloraxis.cmax == 2.0 + fig = px.imshow(img, zmin=2) + assert fig.layout.coloraxis.cmin == 2.0 + assert fig.layout.coloraxis.cmax == 3.0 + + def test_zmin_zmax_range_color_source(): img = img_gray / 100.0 fig1 = px.imshow(img, zmin=0.2, zmax=0.8, binary_string=True)