Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit f6b513f

Browse files
read colors from template
1 parent 0565a5e commit f6b513f

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

‎plotly_express/_core.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import plotly.graph_objs as go
22
from plotly.offline import init_notebook_mode, iplot
3+
import plotly.io as pio
34
from collections import namedtuple, OrderedDict
45
from .colors import qualitative, sequential
56
import math
67

78

89
class PxDefaults(object):
910
def __init__(self):
10-
self.color_discrete_sequence = qualitative.Plotly
11-
self.color_continuous_scale = sequential.Plasma
11+
self.template = None
12+
self.width = None
13+
self.height = 600
14+
self.color_discrete_sequence = None
15+
self.color_continuous_scale = None
1216
self.symbol_sequence = ["circle", "diamond", "square", "x", "cross"]
1317
self.line_dash_sequence = ["solid", "dot", "dash", "longdash", "dashdot"] + [
1418
"longdashdot"
1519
]
16-
self.template = "plotly"
17-
self.width = None
18-
self.height = 600
1920
self.size_max = 20
2021

2122

@@ -569,7 +570,7 @@ def make_trace_spec(args, constructor, attrs, trace_patch):
569570
if "color" in attrs:
570571
if "marker" not in trace_spec.trace_patch:
571572
trace_spec.trace_patch["marker"] = dict()
572-
first_default_color = defaults.color_discrete_sequence[0]
573+
first_default_color = args["color_discrete_sequence"][0]
573574
trace_spec.trace_patch["marker"]["color"] = first_default_color
574575
result.append(trace_spec)
575576
if "trendline" in args and args["trendline"]:
@@ -588,7 +589,8 @@ def one_group(x):
588589
return ""
589590

590591

591-
def infer_config(args, constructor, trace_patch):
592+
def apply_default_cascade(args):
593+
# first we apply px.defaults to unspecified args
592594
for param in (
593595
["color_discrete_sequence", "color_continuous_scale"]
594596
+ ["symbol_sequence", "line_dash_sequence", "template"]
@@ -597,6 +599,43 @@ def infer_config(args, constructor, trace_patch):
597599
if param in args and args[param] is None:
598600
args[param] = getattr(defaults, param)
599601

602+
# load the default template if set, otherwise "plotly"
603+
if args["template"] is None:
604+
if pio.templates.default is not None:
605+
args["template"] = pio.templates.default
606+
else:
607+
args["template"] = "plotly"
608+
609+
# retrieve the actual template if we were given a name
610+
try:
611+
template = pio.templates[args["template"]]
612+
except Exception:
613+
template = args["template"]
614+
615+
# if colors not set explicitly or in px.defaults, defer to a template
616+
# if the template doesn't have one, we set some final fallback defaults
617+
if "color_continuous_scale" in args:
618+
if args["color_continuous_scale"] is None:
619+
try:
620+
args["color_continuous_scale"] = [
621+
x[1] for x in template.layout.colorscale.sequential
622+
]
623+
except AttributeError:
624+
pass
625+
if args["color_continuous_scale"] is None:
626+
args["color_continuous_scale"] = sequential.Plasma
627+
628+
if "color_discrete_sequence" in args:
629+
if args["color_discrete_sequence"] is None:
630+
try:
631+
args["color_discrete_sequence"] = template.layout.colorway
632+
except AttributeError:
633+
pass
634+
if args["color_discrete_sequence"] is None:
635+
args["color_discrete_sequence"] = qualitative.Plotly
636+
637+
638+
def infer_config(args, constructor, trace_patch):
600639
attrables = (
601640
["x", "y", "z", "a", "b", "c", "r", "theta", "size"]
602641
+ ["dimensions", "hover_name", "hover_data", "text", "error_x", "error_x_minus"]
@@ -631,9 +670,7 @@ def infer_config(args, constructor, trace_patch):
631670

632671
sizeref = 0
633672
if "size" in args and args["size"]:
634-
sizeref = args["data_frame"][args["size"]].max() / (
635-
args["size_max"] * args["size_max"]
636-
)
673+
sizeref = args["data_frame"][args["size"]].max() / args["size_max"] ** 2
637674

638675
color_range = None
639676
if "color" in args:
@@ -702,6 +739,7 @@ def infer_config(args, constructor, trace_patch):
702739

703740

704741
def make_figure(args, constructor, trace_patch={}, layout_patch={}):
742+
apply_default_cascade(args)
705743
trace_specs, grouped_mappings, sizeref, color_range = infer_config(
706744
args, constructor, trace_patch
707745
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /