Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ def one_group(x):
return ""


def apply_default_cascade(args, constructor=None):
def apply_default_cascade(args, constructor):
# first we apply px.defaults to unspecified args

for param in defaults.__slots__:
Expand Down Expand Up @@ -1047,16 +1047,15 @@ def apply_default_cascade(args, constructor=None):
else:
trace_type = constructor().type
if trace_data_list := getattr(args["template"].data, trace_type, None):
args["color_discrete_sequence"] = [
trace_specific_colors = [
trace_data.marker.color
for trace_data in trace_data_list
if hasattr(trace_data, "marker")
and hasattr(trace_data.marker, "color")
]
if not args["color_discrete_sequence"] or not any(
args["color_discrete_sequence"]
):
args["color_discrete_sequence"] = None
# If template contains at least one color for this trace type, assign to color_discrete_sequence
if any(trace_specific_colors):
args["color_discrete_sequence"] = trace_specific_colors
# fallback to layout.colorway if trace-specific colors not available
if args["color_discrete_sequence"] is None and args["template"].layout.colorway:
args["color_discrete_sequence"] = args["template"].layout.colorway
Expand Down
25 changes: 14 additions & 11 deletions tests/test_optional/test_px/test_px.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from itertools import permutations
import warnings

import pandas as pd
import plotly.express as px
import plotly.io as pio
import narwhals.stable.v1 as nw
Expand Down Expand Up @@ -227,21 +228,23 @@ def test_px_templates(backend):


def test_px_templates_trace_specific_colors(backend):
import pandas as pd

tips = px.data.tips(return_type=backend)

# trace-specific colors: each trace type uses its own template colors
template = {
"data_histogram": [
{"marker": {"color": "orange"}},
{"marker": {"color": "purple"}},
],
"data_bar": [
{"marker": {"color": "red"}},
{"marker": {"color": "blue"}},
],
"layout_colorway": ["yellow", "green"],
"data": {
"histogram": [
{"marker": {"color": "orange"}},
{"marker": {"color": "purple"}},
],
"bar": [
{"marker": {"color": "red"}},
{"marker": {"color": "blue"}},
],
},
"layout": {
"colorway": ["yellow", "green"],
},
}
# histogram uses histogram colors
fig = px.histogram(tips, x="total_bill", color="sex", template=template)
Expand Down