@@ -51,3 +51,111 @@ def test_custom_data_scatter():
51
51
fig .data [0 ].hovertemplate
52
52
== "sepal_width=%{x}<br>sepal_length=%{y}<br>petal_length=%{customdata[2]}<br>petal_width=%{customdata[3]}<br>species_id=%{customdata[0]}"
53
53
)
54
+
55
+
56
+ def test_px_templates ():
57
+ import plotly .io as pio
58
+ import plotly .graph_objects as go
59
+
60
+ tips = px .data .tips ()
61
+
62
+ # use the normal defaults
63
+ fig = px .scatter ()
64
+ assert fig .layout .template == pio .templates [pio .templates .default ]
65
+
66
+ # respect changes to defaults
67
+ pio .templates .default = "seaborn"
68
+ fig = px .scatter ()
69
+ assert fig .layout .template == pio .templates ["seaborn" ]
70
+
71
+ # special px-level defaults over pio defaults
72
+ pio .templates .default = "seaborn"
73
+ px .defaults .template = "ggplot2"
74
+ fig = px .scatter ()
75
+ assert fig .layout .template == pio .templates ["ggplot2" ]
76
+
77
+ # accept names in args over pio and px defaults
78
+ fig = px .scatter (template = "seaborn" )
79
+ assert fig .layout .template == pio .templates ["seaborn" ]
80
+
81
+ # accept objects in args
82
+ fig = px .scatter (template = {})
83
+ assert fig .layout .template == go .layout .Template ()
84
+
85
+ # read colorway from the template
86
+ fig = px .scatter (
87
+ tips ,
88
+ x = "total_bill" ,
89
+ y = "tip" ,
90
+ color = "sex" ,
91
+ template = dict (layout_colorway = ["red" , "blue" ]),
92
+ )
93
+ assert fig .data [0 ].marker .color == "red"
94
+ assert fig .data [1 ].marker .color == "blue"
95
+
96
+ # default colorway fallback
97
+ fig = px .scatter (tips , x = "total_bill" , y = "tip" , color = "sex" , template = dict ())
98
+ assert fig .data [0 ].marker .color == px .colors .qualitative .D3 [0 ]
99
+ assert fig .data [1 ].marker .color == px .colors .qualitative .D3 [1 ]
100
+
101
+ # pio default template colorway fallback
102
+ pio .templates .default = "seaborn"
103
+ px .defaults .template = None
104
+ fig = px .scatter (tips , x = "total_bill" , y = "tip" , color = "sex" )
105
+ assert fig .data [0 ].marker .color == pio .templates ["seaborn" ].layout .colorway [0 ]
106
+ assert fig .data [1 ].marker .color == pio .templates ["seaborn" ].layout .colorway [1 ]
107
+
108
+ # pio default template colorway fallback
109
+ pio .templates .default = "seaborn"
110
+ px .defaults .template = "ggplot2"
111
+ fig = px .scatter (tips , x = "total_bill" , y = "tip" , color = "sex" )
112
+ assert fig .data [0 ].marker .color == pio .templates ["ggplot2" ].layout .colorway [0 ]
113
+ assert fig .data [1 ].marker .color == pio .templates ["ggplot2" ].layout .colorway [1 ]
114
+
115
+ # don't overwrite top margin when set in template
116
+ fig = px .scatter (title = "yo" )
117
+ assert fig .layout .margin .t is None
118
+
119
+ fig = px .scatter ()
120
+ assert fig .layout .margin .t == 60
121
+
122
+ fig = px .scatter (template = dict (layout_margin_t = 2 ))
123
+ assert fig .layout .margin .t is None
124
+
125
+ # don't force histogram gridlines when set in template
126
+ pio .templates .default = "none"
127
+ px .defaults .template = None
128
+ fig = px .scatter (
129
+ tips , x = "total_bill" , y = "tip" , marginal_x = "histogram" , marginal_y = "histogram"
130
+ )
131
+ assert fig .layout .xaxis2 .showgrid
132
+ assert fig .layout .xaxis3 .showgrid
133
+ assert fig .layout .yaxis2 .showgrid
134
+ assert fig .layout .yaxis3 .showgrid
135
+
136
+ fig = px .scatter (
137
+ tips ,
138
+ x = "total_bill" ,
139
+ y = "tip" ,
140
+ marginal_x = "histogram" ,
141
+ marginal_y = "histogram" ,
142
+ template = dict (layout_yaxis_showgrid = False ),
143
+ )
144
+ assert fig .layout .xaxis2 .showgrid
145
+ assert fig .layout .xaxis3 .showgrid
146
+ assert fig .layout .yaxis2 .showgrid is None
147
+ assert fig .layout .yaxis3 .showgrid is None
148
+
149
+ fig = px .scatter (
150
+ tips ,
151
+ x = "total_bill" ,
152
+ y = "tip" ,
153
+ marginal_x = "histogram" ,
154
+ marginal_y = "histogram" ,
155
+ template = dict (layout_xaxis_showgrid = False ),
156
+ )
157
+ assert fig .layout .xaxis2 .showgrid is None
158
+ assert fig .layout .xaxis3 .showgrid is None
159
+ assert fig .layout .yaxis2 .showgrid
160
+ assert fig .layout .yaxis3 .showgrid
161
+
0 commit comments