From 984ea2f8cf4bb74100c130ad8c5c2caa822d0b70 Mon Sep 17 00:00:00 2001 From: xhl123XHL352 <194903029+xhl123XHL352@users.noreply.github.com> Date: Tue, 24 Nov 2026 06:50:30 +0000 Subject: [PATCH] Accept NumPy scalar positions and counts in sample_colorscale Assisted-by: OpenAI Codex --- CHANGELOG.md | 1 + _plotly_utils/colors/__init__.py | 16 +++++++++------- tests/test_optional/test_px/test_colors.py | 22 +++++++++++++++++++++- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56bb878e296..98bc5f8ad47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Fix `plotly.io.write_images` ignoring `layout.width`/`layout.height` (and the template's) and exporting every figure at the default 700x500 size; it now uses the same width/height priority as `write_image` and `to_image` [[#5784](https://github.com/plotly/plotly.py/pull/5784)], with thanks to @breken-ai for the contribution! - Fix color utilities mutating the list of colors passed to them [[#5836](https://github.com/plotly/plotly.py/pull/5836)], with thanks to @Enesuygurs for the contribution! - Fix Plotly Express (including `px.imshow`) ignoring `facet_row_spacing=0` and `facet_col_spacing=0` and using the default spacing between facets instead [[#5881](https://github.com/plotly/plotly.py/pull/5881)], with thanks to @rishi02102017 for the contribution! +- Fix `sample_colorscale` rejecting NumPy floating-point positions and integer sample counts. ## [7.1.0] - 2026-09-15 diff --git a/_plotly_utils/colors/__init__.py b/_plotly_utils/colors/__init__.py index e3a304c9bb1..901bd49cfc9 100644 --- a/_plotly_utils/colors/__init__.py +++ b/_plotly_utils/colors/__init__.py @@ -884,13 +884,14 @@ def sample_colorscale(colorscale, samplepoints, low=0.0, high=1.0, colortype="rg Interpolates between colors in a colorscale to find the specific colors corresponding to the specified sample values. The colorscale can be specified as a list of `[scale, color]` pairs, as a list of colors, or as a named - plotly colorscale. The samplepoints can be specefied as an iterable of specific - points in the range [0.0, 1.0], or as an integer number of points which will - be spaced equally between the low value (default 0.0) and the high value - (default 1.0). The output is a list of colors, formatted according to the - specified colortype. + plotly colorscale. Pass a real-valued position or an iterable of positions in + [0.0, 1.0], or pass an integer to request that many evenly spaced positions + between low (default 0.0) and high (default 1.0). NumPy floating-point and + integer scalars are also accepted. The output is a list of colors formatted + according to colortype. """ from bisect import bisect_left + from numbers import Integral, Real try: validate_colorscale(colorscale) @@ -905,11 +906,12 @@ def sample_colorscale(colorscale, samplepoints, low=0.0, high=1.0, colortype="rg colors = colorscale_to_colors(colorscale) colors = validate_colors(colors, colortype="tuple") - if isinstance(samplepoints, int): + if isinstance(samplepoints, Integral): + samplepoints = int(samplepoints) samplepoints = [ low + idx / (samplepoints - 1) * (high - low) for idx in range(samplepoints) ] - elif isinstance(samplepoints, float): + elif isinstance(samplepoints, Real): samplepoints = [samplepoints] sampled_colors = [] diff --git a/tests/test_optional/test_px/test_colors.py b/tests/test_optional/test_px/test_colors.py index d41dc7aa6e5..6706fd6272e 100644 --- a/tests/test_optional/test_px/test_colors.py +++ b/tests/test_optional/test_px/test_colors.py @@ -1,6 +1,26 @@ -import plotly.express as px import inspect +import numpy as np +import pytest + +import plotly.express as px + + +@pytest.mark.parametrize( + ("samplepoints", "python_samplepoints"), + [ + (np.float16(0.625), 0.625), + (np.float32(0.625), 0.625), + (np.longdouble(0.625), 0.625), + (np.int32(4), 4), + (np.uint64(4), 4), + ], +) +def test_sample_colorscale_accepts_numpy_scalars(samplepoints, python_samplepoints): + expected = px.colors.sample_colorscale("Viridis", python_samplepoints) + + assert px.colors.sample_colorscale("Viridis", samplepoints) == expected + def test_reversed_colorscale(): fig1 = px.scatter(