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
commented
Nov 27, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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)
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
- 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
changed the title
Trace-specific colors
[Fix] Support trace-specific color sequences in Plotly Express via templates
Dec 1, 2025
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
@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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
- 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
Uh oh!
There was an error while loading. Please reload this page.
- 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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
emilykl
commented
Dec 4, 2025
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
@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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
antonymilne reacted with heart emoji
|
@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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
1 more reviewer
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.
Milestone
No milestone
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.
|