Repository navigation
Fix ZeroDivisionError when requesting a single color from palette helpers - #5922
Open
rashedrana wants to merge 2 commits into
Open
rashedrana wants to merge 2 commits into
rashedrana wants to merge 2 commits into
Conversation
atishay-kasliwal
left a comment
There was a problem hiding this comment.
n_colors(..., 1) now returns the low colour and sample_colorscale('Viridis', 1) returns rgb(68, 1, 84) (with low=0.5, the 0.5 colour; with colortype='tuple', a tuple); master raised ZeroDivisionError for each. 0 and 3 samples are unchanged. For whoever triages: #5882, opened 2026-11-15 for the same issue, makes the same two changes.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to issue
Fixes #5805
Description of change
sample_colorscaleandn_colorsraiseZeroDivisionErrorwhen asked for exactly one color, because both divide by the requested count minus one. This breaks palettes sized to a dataset's group count when there is exactly one group.For a single requested color there is no interval to step through:
n_colorsnow uses a zero increment so it returns the low color, andsample_colorscaletakes its single sample at thelowend of the interval. Results for zero and for two or more colors are unchanged.Prepared with AI assistance and verified locally by me.
Demo
Not applicable. This change fixes color utility functions and does not introduce visual changes to Plotly charts.
Testing strategy
Added regression tests
test_sample_colorscale_single_colorandtest_n_colors_single_colorto tests/test_core/test_colors/test_colors.py. Both fail on the original code with ZeroDivisionError and pass with the fix. Full file: 11 passed.Additional information (optional)
Repro from the issue, on current main:
Guidelines