GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Fix] Support trace-specific color sequences in Plotly Express via templates by antonymilne · Pull Request #5437 · plotly/plotly.py

Conversation

@antonymilne

@antonymilne antonymilne commented Nov 27, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

huong-li-nguyen reacted with rocket emoji
All reactions
  • 🚀 1 reaction
antonymilne added 5 commits November 26, 2025 17:01
@antonymilne
Add trace-specific color sequence support from template.data
965b3bd
- 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
@antonymilne
Support trace-specific color sequences in Plotly Express via templates
83faec8
- 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
@antonymilne
Add trace-specific color sequences in Plotly Express via templates
7bdec7f
- 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
Minor
befea7d
@antonymilne
Format code with ruff
d578ab4
@antonymilne antonymilne changed the title Trace-specific colors [Fix] Support trace-specific color sequences in Plotly Express via templates Dec 1, 2025
@antonymilne
Add CHANGELOG entry for trace-specific color sequences fix
a2dc490
@antonymilne
antonymilne marked this pull request as ready for review December 1, 2025 12:00
@antonymilne
Update changelog
74543f3
@antonymilne antonymilne mentioned this pull request Dec 1, 2025
[Bug] Plotly Express does not respect trace-specific color sequences defined in template #5416
Closed
@emilykl
emilykl self-requested a review December 2, 2025 16:43
@emilykl emilykl self-assigned this Dec 2, 2025
emilykl
emilykl reviewed Dec 2, 2025
View reviewed changes
Comment thread plotly/express/_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

emilykl
emilykl reviewed Dec 2, 2025
View reviewed changes
Comment thread plotly/express/_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

emilykl
emilykl reviewed Dec 2, 2025
View reviewed changes
Comment thread tests/test_optional/test_px/test_px.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

emilykl
emilykl reviewed Dec 2, 2025
View reviewed changes
Comment thread tests/test_optional/test_px/test_px.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@emilykl

emilykl commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

@antonymilne This is a great PR, thank you for the contribution!

I left a few small style/clarity comments, once those are resolved I'll approve.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@antonymilne
Refactor trace-specific color handling in apply_default_cascade
675c668
- Make constructor parameter required (was optional with None default)
- Refactor trace-specific color extraction to only assign when colors are found
- Improve code clarity by checking for non-empty color list before assignment
emilykl
emilykl reviewed Dec 4, 2025
View reviewed changes
Comment thread plotly/express/_core.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

antonymilne and others added 4 commits December 4, 2025 21:23
@antonymilne
Remove redundant color_discrete_sequence check in apply_default_cascade
c24b05c
- Remove redundant check that was setting color_discrete_sequence to None
- The check is unnecessary since we only assign trace_specific_colors when any() is True
- Fallback logic to layout.colorway and qualitative.D3 remains intact
@antonymilne @emilykl
Update tests/test_optional/test_px/test_px.py
076ff14
Co-authored-by: Emily KL <[email protected]>
@antonymilne
Ruff
a3853d2
@antonymilne
Merge branch 'main' into main
af1546c
@antonymilne

antonymilne commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

@emilykl thanks very much for your review! I've implemented all your suggested changes exactly as you wrote them, with one small simplification where I changed if trace_specific_colors and any(trace_specific_colors) into just if any(trace_specific_colors).

I messed up the commits a bit but you can see all the changes since your last review here: https://github.com/plotly/plotly.py/pull/5437/files/74543f301be86ab121a8069a506ced9aba2a2d53..a3853d2afc113cea2fda3ab98b435e745823abfd.

P.S. if you're in the mood for reviewing, I have another small PR here 😀 #5439

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@emilykl

emilykl commented Dec 4, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@antonymilne One final change needed -- in plotly/express/_imshow.py, modify the apply_default_cascade call to pass constructor=None. I suspect that will fix the failing tests (although I can't see the failure details because CircleCI seems to be having an outage right now). Then this will be ready to merge!

Thanks for your work on this bugfix, and for the additional PR! I will do my best to review tomorrow or early next week.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@antonymilne
Fix imshow
e8339e0
@antonymilne

antonymilne commented Dec 5, 2025

Copy link
Copy Markdown
Contributor Author

Oops yes sorry, I made the change to imshow.py before but forgot to push it 🤦

There's a couple of failing tests but I'm pretty sure they're not my fault - I was getting similar messages in #5439. Please could you take a look? 🙏 Maybe they're just flaky and need rerunning? I'm not sure.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

emilykl
emilykl approved these changes Dec 5, 2025
View reviewed changes

@emilykl emilykl left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks @antonymilne ! 🚀

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

antonymilne reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@emilykl

emilykl commented Dec 5, 2025

Copy link
Copy Markdown
Contributor

@antonymilne Awesome!

You're right, those failing tests are not related to your PR. I'll go ahead and merge.

antonymilne reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@emilykl
emilykl merged commit adfdfcd into plotly:main Dec 5, 2025
7 of 8 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@emilykl emilykl mentioned this pull request Dec 5, 2025
Fix failing docs build by updating call to color_discrete_sequence with new call signature #5442
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@emilykl emilykl emilykl approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@emilykl emilykl

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[Bug] Plotly Express does not respect trace-specific color sequences defined in template

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@antonymilne @emilykl
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.