Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 9 additions & 7 deletions _plotly_utils/colors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = []
Expand Down
22 changes: 21 additions & 1 deletion tests/test_optional/test_px/test_colors.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down