Skip to content

Conversation

@antonymilne
Copy link

@antonymilne antonymilne commented Nov 27, 2025

Summary

Fixes #5416.

You can define color sequences in a template on a per-trace basis via template.data.<trace_type>, but these were ignored by Plotly Express. Now they are used.

Changes

  • Modified apply_default_cascade() to check for trace-specific colors in template.data.<trace_type> before falling back to template.layout.colorway or default qualitative palette
  • Added test_px_templates_trace_specific_colors

Example Usage

See here for a demonstration of all (I think?) applicable trace types, i.e. those that support marker.color.

import plotly.express as px
import plotly.graph_objects as go

tips = px.data.tips()

# Create a custom template with trace-specific colors
custom_template = go.layout.Template(
    data={
        "histogram": [
            go.Histogram(marker=dict(color="orange")),
            go.Histogram(marker=dict(color="purple")),
        ],
        "scatter": [
            go.Scatter(marker=dict(color="cyan")),
            go.Scatter(marker=dict(color="magenta")),
        ],
    },
    layout=go.Layout(colorway=["yellow", "green"]),
)
template = pio.templates.merge_templates("plotly", custom_template)

# Histogram uses histogram-specific colors
fig1 = px.histogram(tips, x="total_bill", color="sex", template=template)

# Scatter uses scatter-specific colors
fig2 = px.scatter(tips, x="total_bill", y="tip", color="sex", template=template)

# Box plot falls back to layout.colorway
fig3 = px.box(tips, x="day", y="total_bill", color="sex", template=template)
changelog_histogram changelog_scatter changelog_box

Code PR

  • I have read through the contributing notes and understand the structure of the package. In particular, if my PR modifies code of plotly.graph_objects, my modifications concern the code generator and not the generated files.
  • I have added tests or modified existing tests.
  • For a new feature, I have added documentation examples (please see the doc checklist as well).
  • I have added a CHANGELOG entry if changing anything substantial.
  • For a new feature or a change in behavior, I have updated the relevant docstrings in the code.

- Check template.data.<trace_type> for marker.color or line.color before falling back to template.layout.colorway
- Handle timeline special case (maps to bar trace type)
- Use marker colors first, fall back to line colors if no markers found
- Fixes issue plotly#5416
- Modify apply_default_cascade to check template.data.<trace_type> for marker.color or line.color
- Fallback to template.layout.colorway if trace-specific colors not found
- Add comprehensive tests for trace-specific color sequences
- Handle timeline special case (maps to bar trace type)
- Follow existing patterns for symbol_sequence and line_dash_sequence

Fixes plotly#5416
- Modify apply_default_cascade to read colors from template.data.<trace_type>
- Prioritize trace-specific colors over layout.colorway
- Add special case for timeline constructor (maps to bar trace type)
- Add comprehensive tests for trace-specific color sequences
- Test trace type isolation, fallback behavior, and timeline special case
@antonymilne antonymilne changed the title Trace-specific colors [Fix] Support trace-specific color sequences in Plotly Express via templates Dec 1, 2025
@antonymilne antonymilne marked this pull request as ready for review December 1, 2025 12:00
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.

[Feature request] Trace-specific color sequences in Plotly Express via templates

1 participant