1
1
import plotly .graph_objs as go
2
2
from plotly .offline import init_notebook_mode , iplot
3
+ import plotly .io as pio
3
4
from collections import namedtuple , OrderedDict
4
5
from .colors import qualitative , sequential
5
6
import math
6
7
7
8
8
9
class PxDefaults (object ):
9
10
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
12
16
self .symbol_sequence = ["circle" , "diamond" , "square" , "x" , "cross" ]
13
17
self .line_dash_sequence = ["solid" , "dot" , "dash" , "longdash" , "dashdot" ] + [
14
18
"longdashdot"
15
19
]
16
- self .template = "plotly"
17
- self .width = None
18
- self .height = 600
19
20
self .size_max = 20
20
21
21
22
@@ -569,7 +570,7 @@ def make_trace_spec(args, constructor, attrs, trace_patch):
569
570
if "color" in attrs :
570
571
if "marker" not in trace_spec .trace_patch :
571
572
trace_spec .trace_patch ["marker" ] = dict ()
572
- first_default_color = defaults . color_discrete_sequence [0 ]
573
+ first_default_color = args [ " color_discrete_sequence" ] [0 ]
573
574
trace_spec .trace_patch ["marker" ]["color" ] = first_default_color
574
575
result .append (trace_spec )
575
576
if "trendline" in args and args ["trendline" ]:
@@ -588,7 +589,8 @@ def one_group(x):
588
589
return ""
589
590
590
591
591
- def infer_config (args , constructor , trace_patch ):
592
+ def apply_default_cascade (args ):
593
+ # first we apply px.defaults to unspecified args
592
594
for param in (
593
595
["color_discrete_sequence" , "color_continuous_scale" ]
594
596
+ ["symbol_sequence" , "line_dash_sequence" , "template" ]
@@ -597,6 +599,43 @@ def infer_config(args, constructor, trace_patch):
597
599
if param in args and args [param ] is None :
598
600
args [param ] = getattr (defaults , param )
599
601
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 ):
600
639
attrables = (
601
640
["x" , "y" , "z" , "a" , "b" , "c" , "r" , "theta" , "size" ]
602
641
+ ["dimensions" , "hover_name" , "hover_data" , "text" , "error_x" , "error_x_minus" ]
@@ -631,9 +670,7 @@ def infer_config(args, constructor, trace_patch):
631
670
632
671
sizeref = 0
633
672
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
637
674
638
675
color_range = None
639
676
if "color" in args :
@@ -702,6 +739,7 @@ def infer_config(args, constructor, trace_patch):
702
739
703
740
704
741
def make_figure (args , constructor , trace_patch = {}, layout_patch = {}):
742
+ apply_default_cascade (args )
705
743
trace_specs , grouped_mappings , sizeref , color_range = infer_config (
706
744
args , constructor , trace_patch
707
745
)
0 commit comments