Skip to content

Commit ecb75a0

Browse files
FBumannclaude
andcommitted
fix: continuous colorscale strings with discrete-only chart types
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>
1 parent 63f37a7 commit ecb75a0

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

xarray_plotly/common.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,18 @@ def _get_qualitative_scale_names() -> frozenset[str]:
251251
)
252252

253253

254-
def resolve_colors(colors: Colors, px_kwargs: dict[str, Any]) -> dict[str, Any]:
254+
def _sample_colorscale(name: str, n: int = 10) -> list[str]:
255+
"""Sample *n* evenly-spaced colors from a named Plotly colorscale."""
256+
scale = px.colors.get_colorscale(name)
257+
return px.colors.sample_colorscale(scale, [i / (n - 1) for i in range(n)])
258+
259+
260+
def resolve_colors(
261+
colors: Colors,
262+
px_kwargs: dict[str, Any],
263+
*,
264+
discrete_only: bool = False,
265+
) -> dict[str, Any]:
255266
"""Map unified `colors` parameter to appropriate Plotly px_kwargs.
256267
257268
Direct color_* kwargs take precedence and trigger a warning if
@@ -260,6 +271,10 @@ def resolve_colors(colors: Colors, px_kwargs: dict[str, Any]) -> dict[str, Any]:
260271
Args:
261272
colors: Unified color specification (str, list, dict, or None).
262273
px_kwargs: Existing kwargs to pass to Plotly Express.
274+
discrete_only: If True, continuous colorscale strings are sampled
275+
into a discrete sequence instead of setting
276+
``color_continuous_scale``. Use for chart types that only
277+
accept discrete color parameters (line, bar, area, box, pie).
263278
264279
Returns:
265280
Updated px_kwargs with color parameters injected.
@@ -284,6 +299,9 @@ def resolve_colors(colors: Colors, px_kwargs: dict[str, Any]) -> dict[str, Any]:
284299
# Check if it's a qualitative (discrete) palette name
285300
if colors in _get_qualitative_scale_names():
286301
px_kwargs["color_discrete_sequence"] = getattr(px.colors.qualitative, colors)
302+
elif discrete_only:
303+
# Sample from continuous scale into a discrete sequence
304+
px_kwargs["color_discrete_sequence"] = _sample_colorscale(colors)
287305
else:
288306
# Assume continuous scale
289307
px_kwargs["color_continuous_scale"] = colors

xarray_plotly/plotting.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def line(
8282
-------
8383
plotly.graph_objects.Figure
8484
"""
85-
px_kwargs = resolve_colors(colors, px_kwargs)
85+
px_kwargs = resolve_colors(colors, px_kwargs, discrete_only=True)
8686
slots = assign_slots(
8787
list(darray.dims),
8888
"line",
@@ -329,7 +329,7 @@ def fast_bar(
329329
-------
330330
plotly.graph_objects.Figure
331331
"""
332-
px_kwargs = resolve_colors(colors, px_kwargs)
332+
px_kwargs = resolve_colors(colors, px_kwargs, discrete_only=True)
333333
slots = assign_slots(
334334
list(darray.dims),
335335
"fast_bar",
@@ -410,7 +410,7 @@ def area(
410410
-------
411411
plotly.graph_objects.Figure
412412
"""
413-
px_kwargs = resolve_colors(colors, px_kwargs)
413+
px_kwargs = resolve_colors(colors, px_kwargs, discrete_only=True)
414414
slots = assign_slots(
415415
list(darray.dims),
416416
"area",
@@ -487,7 +487,7 @@ def box(
487487
-------
488488
plotly.graph_objects.Figure
489489
"""
490-
px_kwargs = resolve_colors(colors, px_kwargs)
490+
px_kwargs = resolve_colors(colors, px_kwargs, discrete_only=True)
491491
slots = assign_slots(
492492
list(darray.dims),
493493
"box",
@@ -746,7 +746,7 @@ def pie(
746746
-------
747747
plotly.graph_objects.Figure
748748
"""
749-
px_kwargs = resolve_colors(colors, px_kwargs)
749+
px_kwargs = resolve_colors(colors, px_kwargs, discrete_only=True)
750750
slots = assign_slots(
751751
list(darray.dims),
752752
"pie",

0 commit comments

Comments
 (0)