Skip to content

fix: continuous colorscale strings with discrete-only chart types#46

Merged
FBumann merged 1 commit intomainfrom
fix/continuous-colorscale-discrete-charts
Mar 9, 2026
Merged

fix: continuous colorscale strings with discrete-only chart types#46
FBumann merged 1 commit intomainfrom
fix/continuous-colorscale-discrete-charts

Conversation

@FBumann
Copy link
Owner

@FBumann FBumann commented Mar 9, 2026

Summary

  • Passing a continuous colorscale string (e.g., colors='turbo') to area(), line(), fast_bar(), box(), or pie() previously failed because resolve_colors set color_continuous_scale, which these Plotly Express functions don't accept.
  • Added discrete_only parameter to resolve_colors() that samples from the continuous scale into a color_discrete_sequence instead.
  • bar(), scatter(), and imshow() are unchanged (they natively support color_continuous_scale).

Test plan

  • All 121 existing tests pass
  • Manual smoke test: colors='turbo' works on all 8 chart types

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Added ability to sample continuous color scales into discrete palettes when a specific number of colors is requested.
  • Improvements

    • Enhanced color palette handling across line, bar, area, box, and pie charts to respect the actual number of series, producing more consistent and distinguishable discrete color sequences.

@FBumann FBumann force-pushed the fix/continuous-colorscale-discrete-charts branch 2 times, most recently from ecb75a0 to 6a971a3 Compare March 9, 2026 12:59
@coderabbitai
Copy link

coderabbitai bot commented Mar 9, 2026

Warning

Rate limit exceeded

@FBumann has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 4 minutes and 23 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0bf0c574-6e1d-49de-adf1-dd698d87ba78

📥 Commits

Reviewing files that changed from the base of the PR and between fc2fe21 and 23edbc3.

📒 Files selected for processing (2)
  • xarray_plotly/common.py
  • xarray_plotly/plotting.py
📝 Walkthrough

Walkthrough

Added a helper to sample discrete colors from Plotly colorscales and extended resolve_colors() to accept n_colors (keyword-only). Plotting functions now pass the relevant dimension size as n_colors when resolving colors.

Changes

Cohort / File(s) Summary
Color sampling & resolver
xarray_plotly/common.py
Added _sample_colorscale(name: str, n: int) -> list[str]. Updated `resolve_colors(colors, px_kwargs, *, n_colors: int
Plotting callers
xarray_plotly/plotting.py
Updated plotting functions (line, fast_bar, area, box, pie) to compute the color/names dimension size (n_colors) from slots/darray.sizes and pass n_colors=n_colors to resolve_colors() instead of calling it without that parameter.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I nibble at palettes, pick each hue,
Sampling rainbows, one, two, and few,
Plots bloom with counted colors bright,
Hop-scotch through scales, a tidy sight,
A rabbit’s cheer for every view 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and specifically describes the main fix: handling continuous colorscale strings with chart types that only support discrete colors.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/continuous-colorscale-discrete-charts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@xarray_plotly/common.py`:
- Around line 275-278: Update the docstring for the discrete_only parameter to
remove "bar" from the list of chart types that sample continuous colors into a
discrete sequence; specifically, edit the text near the discrete_only
description in xarray_plotly/common.py so it only lists line(), area(), box(),
and pie() (since bar() calls resolve_colors(colors, px_kwargs) without
discrete_only=True). Leave the rest of the description unchanged.
- Around line 254-258: The _sample_colorscale function is sampling a fixed n
(default 10) which causes color clustering for small categorical dims; change
resolve_colors so sampling uses the actual category count: either call
resolve_colors/_sample_colorscale after assign_slots so you can read the
cardinality from darray.sizes[color_dim], or refactor resolve_colors to accept
the resolved darray and the color dimension name and compute n =
int(darray.sizes[color_dim]) before calling _sample_colorscale; update all call
sites (including the calls that currently pass only colors when
discrete_only=True) to provide the proper n or the darray+color dim so samples
span the full colorscale.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f275bb3a-1782-4698-9ac8-ed1ee7b15780

📥 Commits

Reviewing files that changed from the base of the PR and between 63f37a7 and 6a971a3.

📒 Files selected for processing (2)
  • xarray_plotly/common.py
  • xarray_plotly/plotting.py

@FBumann FBumann force-pushed the fix/continuous-colorscale-discrete-charts branch 4 times, most recently from f5b74b8 to 40f2a6d Compare March 9, 2026 13:25
Passing a continuous colorscale like `colors='turbo'` to area(), line(),
fast_bar(), box(), or pie() previously failed because `resolve_colors`
set `color_continuous_scale` which these px functions don't accept. Now
samples from the continuous scale into a discrete sequence for these
chart types. bar() and scatter() natively support continuous scales so
are left unchanged.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@FBumann FBumann force-pushed the fix/continuous-colorscale-discrete-charts branch from 40f2a6d to 23edbc3 Compare March 9, 2026 13:27
@FBumann FBumann merged commit cc865b5 into main Mar 9, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant