Skip to content

Accept NumPy scalar positions and counts in sample_colorscale - #5900

Open
xhl123XHL352 wants to merge 1 commit into
plotly:mainfrom
xhl123XHL352:xhl/accept-numpy-scalar-color-samples
Open

xhl123XHL352 wants to merge 1 commit into
plotly:mainfrom
xhl123XHL352:xhl/accept-numpy-scalar-color-samples

Conversation

@xhl123XHL352

@xhl123XHL352 xhl123XHL352 commented Nov 24, 2026 •

Copy link
Copy Markdown

Link to issue

Minimal reproduction

Description of change

sample_colorscale raises TypeError when passed a numpy.float32 position or a numpy.int32 sample count. Recognize real and integral scalars so NumPy numeric scalars follow the same paths as Python floats and integers.

Demo

import numpy as np
from plotly.colors import sample_colorscale

sample_colorscale('Viridis', np.float32(0.625))

The call returns ['rgb(45, 174, 127)'].

Testing strategy

The five NumPy regression cases failed on the base revision and passed with the fix. Ran pytest -q tests/test_core/test_colors tests/test_plotly_utils/colors tests/test_optional/test_px/test_colors.py (36 passed).

The modified Python files pass checks and formatting with the repository's pinned Ruff 0.11.12.

Additional information

Prepared and verified with assistance from OpenAI Codex.

Guidelines

Assisted-by: OpenAI Codex <noreply@openai.com>

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sample_colorscale('Viridis', x) with np.float32(0.625), np.int64(4) and np.uint8(3) now gives the same colors as the Python float / int; master raised TypeError: ... object is not iterable. np.float64, lists and plain numbers are unchanged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants