Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

read colors from template #74

Merged
merged 1 commit into from
May 10, 2019
Merged
Changes from all 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
58 changes: 48 additions & 10 deletions plotly_express/_core.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
import plotly.io as pio
from collections import namedtuple, OrderedDict
from .colors import qualitative, sequential
import math


class PxDefaults(object):
def __init__(self):
self.color_discrete_sequence = qualitative.Plotly
self.color_continuous_scale = sequential.Plasma
self.template = None
self.width = None
self.height = 600
self.color_discrete_sequence = None
self.color_continuous_scale = None
self.symbol_sequence = ["circle", "diamond", "square", "x", "cross"]
self.line_dash_sequence = ["solid", "dot", "dash", "longdash", "dashdot"] + [
"longdashdot"
]
self.template = "plotly"
self.width = None
self.height = 600
self.size_max = 20


Expand Down Expand Up @@ -569,7 +570,7 @@ def make_trace_spec(args, constructor, attrs, trace_patch):
if "color" in attrs:
if "marker" not in trace_spec.trace_patch:
trace_spec.trace_patch["marker"] = dict()
first_default_color = defaults.color_discrete_sequence[0]
first_default_color = args["color_discrete_sequence"][0]
trace_spec.trace_patch["marker"]["color"] = first_default_color
result.append(trace_spec)
if "trendline" in args and args["trendline"]:
Expand All @@ -588,7 +589,8 @@ def one_group(x):
return ""


def infer_config(args, constructor, trace_patch):
def apply_default_cascade(args):
# first we apply px.defaults to unspecified args
for param in (
["color_discrete_sequence", "color_continuous_scale"]
+ ["symbol_sequence", "line_dash_sequence", "template"]
Expand All @@ -597,6 +599,43 @@ def infer_config(args, constructor, trace_patch):
if param in args and args[param] is None:
args[param] = getattr(defaults, param)

# load the default template if set, otherwise "plotly"
if args["template"] is None:
if pio.templates.default is not None:
args["template"] = pio.templates.default
else:
args["template"] = "plotly"

# retrieve the actual template if we were given a name
try:
template = pio.templates[args["template"]]
except Exception:
template = args["template"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the idea here to catch a go.layout.Template object being passed in directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah


# if colors not set explicitly or in px.defaults, defer to a template
# if the template doesn't have one, we set some final fallback defaults
if "color_continuous_scale" in args:
if args["color_continuous_scale"] is None:
try:
args["color_continuous_scale"] = [
x[1] for x in template.layout.colorscale.sequential
]
except AttributeError:
pass
if args["color_continuous_scale"] is None:
args["color_continuous_scale"] = sequential.Plasma

if "color_discrete_sequence" in args:
if args["color_discrete_sequence"] is None:
try:
args["color_discrete_sequence"] = template.layout.colorway
except AttributeError:
pass
if args["color_discrete_sequence"] is None:
args["color_discrete_sequence"] = qualitative.Plotly


def infer_config(args, constructor, trace_patch):
attrables = (
["x", "y", "z", "a", "b", "c", "r", "theta", "size"]
+ ["dimensions", "hover_name", "hover_data", "text", "error_x", "error_x_minus"]
Expand Down Expand Up @@ -631,9 +670,7 @@ def infer_config(args, constructor, trace_patch):

sizeref = 0
if "size" in args and args["size"]:
sizeref = args["data_frame"][args["size"]].max() / (
args["size_max"] * args["size_max"]
)
sizeref = args["data_frame"][args["size"]].max() / args["size_max"] ** 2

color_range = None
if "color" in args:
Expand Down Expand Up @@ -702,6 +739,7 @@ def infer_config(args, constructor, trace_patch):


def make_figure(args, constructor, trace_patch={}, layout_patch={}):
apply_default_cascade(args)
trace_specs, grouped_mappings, sizeref, color_range = infer_config(
args, constructor, trace_patch
)
Expand Down