@@ -51,11 +51,11 @@ gapminder = px.data.gapminder()
51
51
gapminder_2007 = gapminder.query(" year==2007" )
52
52
53
53
for template in [" plotly" , " plotly_white" , " plotly_dark" , " ggplot2" , " seaborn" , " none" ]:
54
- px.scatter(gapminder_2007,
55
- x = " gdpPercap" , y = " lifeExp" , size = " pop" , color = " continent" ,
56
- log_x = True , size_max = 60 ,
57
- template = template, title = " Gapminder 2007: '%s ' theme" % template
58
- ) .show()
54
+ fig = px.scatter(gapminder_2007,
55
+ x = " gdpPercap" , y = " lifeExp" , size = " pop" , color = " continent" ,
56
+ log_x = True , size_max = 60 ,
57
+ template = template, title = " Gapminder 2007: '%s ' theme" % template)
58
+ fig .show()
59
59
```
60
60
61
61
### Specifying themes to graph object figures
@@ -71,12 +71,13 @@ fig = go.Figure(
71
71
data = go.Surface(z = z_data.values),
72
72
layout = go.Layout(
73
73
title = " Mt Bruno Elevation" ,
74
- width = 500 ,
74
+ width = 500 ,
75
75
height = 500 ,
76
- ))
76
+ ))
77
77
78
78
for template in [" plotly" , " plotly_white" , " plotly_dark" , " ggplot2" , " seaborn" , " none" ]:
79
- fig.update_layout(template = template, title = " Mt Bruno Elevation: '%s ' theme" % template).show()
79
+ fig.update_layout(template = template, title = " Mt Bruno Elevation: '%s ' theme" % template)
80
+ fig.show()
80
81
```
81
82
82
83
### Specifying a default themes
@@ -93,11 +94,11 @@ pio.templates.default = "plotly_white"
93
94
gapminder = px.data.gapminder()
94
95
gapminder_2007 = gapminder.query(" year==2007" )
95
96
96
- px.scatter(gapminder_2007,
97
- x = " gdpPercap" , y = " lifeExp" , size = " pop" , color = " continent" ,
98
- log_x = True , size_max = 60 ,
99
- title = " Gapminder 2007: current default theme"
100
- ) .show()
97
+ fig = px.scatter(gapminder_2007,
98
+ x = " gdpPercap" , y = " lifeExp" , size = " pop" , color = " continent" ,
99
+ log_x = True , size_max = 60 ,
100
+ title = " Gapminder 2007: current default theme" )
101
+ fig .show()
101
102
```
102
103
103
104
### Disable default theming
@@ -125,10 +126,10 @@ large_rockwell_template = go.layout.Template(
125
126
layout = go.Layout(dict (title = dict (font = dict (family = " Rockwell" , size = 24 ))))
126
127
)
127
128
128
- ( go.Figure()
129
- .update_layout(title = " Figure Title" ,
130
- template = large_rockwell_template)
131
- ) .show()
129
+ fig = go.Figure()
130
+ fig .update_layout(title = " Figure Title" ,
131
+ template = large_rockwell_template)
132
+ fig .show()
132
133
```
133
134
134
135
### The template data property
@@ -142,10 +143,10 @@ import plotly.graph_objects as go
142
143
diamond_template = go.layout.Template()
143
144
diamond_template.data.scatter = [go.Scatter(marker = dict (symbol = " diamond" , size = 20 ))]
144
145
145
- ( go.Figure()
146
- .update_layout(template = diamond_template)
147
- .add_scatter(y = [2 , 1 , 3 ], mode = " markers" )
148
- ) .show()
146
+ fig = go.Figure()
147
+ fig .update_layout(template = diamond_template)
148
+ fig .add_scatter(y = [2 , 1 , 3 ], mode = " markers" )
149
+ fig .show()
149
150
```
150
151
151
152
If a trace type property is set to a list of more than one trace, then the default properties are cycled as more traces are added to the figure. Here is an example that creates a template that cycles the default marker symbol for scatter traces, and then constructs a figure that uses this template.
@@ -159,14 +160,13 @@ symbol_template.data.scatter = [
159
160
go.Scatter(marker = dict (symbol = " square" , size = 10 )),
160
161
go.Scatter(marker = dict (symbol = " circle" , size = 10 )),
161
162
]
162
-
163
- (go.Figure()
164
- .update_layout(template = symbol_template)
165
- .add_scatter(y = [1 , 2 , 3 ], mode = " markers" , name = " first" )
166
- .add_scatter(y = [2 , 3 , 4 ], mode = " markers" , name = " second" )
167
- .add_scatter(y = [3 , 4 , 5 ], mode = " markers" , name = " third" )
168
- .add_scatter(y = [4 , 5 , 6 ], mode = " markers" , name = " forth" )
169
- ).show()
163
+ fig = go.Figure()
164
+ fig.update_layout(template = symbol_template)
165
+ fig.add_scatter(y = [1 , 2 , 3 ], mode = " markers" , name = " first" )
166
+ fig.add_scatter(y = [2 , 3 , 4 ], mode = " markers" , name = " second" )
167
+ fig.add_scatter(y = [3 , 4 , 5 ], mode = " markers" , name = " third" )
168
+ fig.add_scatter(y = [4 , 5 , 6 ], mode = " markers" , name = " forth" )
169
+ fig.show()
170
170
```
171
171
172
172
Note that because we built the template with a list of 3 scatter trace graph objects (one each for the diamond, square, and circle symbols), the forth scatter trace in the figure cycles around and takes on the defaults specified in the first template trace (The diamond symbol).
@@ -184,16 +184,15 @@ import plotly.graph_objects as go
184
184
185
185
annotation_template = go.layout.Template()
186
186
annotation_template.layout.annotationdefaults = go.layout.Annotation(font = dict (color = " crimson" ))
187
-
188
- (go.Figure()
189
- .update_layout(
187
+ fig = go.Figure()
188
+ fig.update_layout(
190
189
template = annotation_template,
191
190
annotations = [
192
191
go.layout.Annotation(text = " Look Here" , x = 1 , y = 1 ),
193
192
go.layout.Annotation(text = " Look There" , x = 2 , y = 2 )
194
193
]
195
194
)
196
- ) .show()
195
+ fig .show()
197
196
```
198
197
199
198
### Including array elements in a theme
@@ -223,9 +222,9 @@ draft_template.layout.annotations = [
223
222
)
224
223
]
225
224
226
- ( go.Figure()
227
- .update_layout(template = draft_template)
228
- ) .show()
225
+ fig = go.Figure()
226
+ fig .update_layout(template = draft_template)
227
+ fig .show()
229
228
```
230
229
231
230
### Customizing theme array elements in a figure
@@ -252,17 +251,17 @@ draft_template.layout.annotations = [
252
251
)
253
252
]
254
253
255
- ( go.Figure()
256
- .update_layout(
257
- template = draft_template,
258
- annotations = [
259
- go.layout.Annotation(
260
- templateitemname = " draft watermark" ,
261
- text = " CONFIDENTIAL" ,
262
- )
263
- ]
264
- )
265
- ) .show()
254
+ fig = go.Figure()
255
+ fig .update_layout(
256
+ template = draft_template,
257
+ annotations = [
258
+ go.layout.Annotation(
259
+ templateitemname = " draft watermark" ,
260
+ text = " CONFIDENTIAL" ,
261
+ )
262
+ ]
263
+ )
264
+ fig .show()
266
265
```
267
266
268
267
### Registering themes as named templates
@@ -291,9 +290,9 @@ pio.templates["draft"] = go.layout.Template(
291
290
]
292
291
)
293
292
294
- ( go.Figure()
295
- .update_layout(template = " draft" )
296
- ) .show()
293
+ fig = go.Figure()
294
+ fig .update_layout(template = " draft" )
295
+ fig .show()
297
296
```
298
297
299
298
It is also possible to set your own custom template as the default so that you do not need to pass it by name when constructing graph object figures or calling plotly express functions.
@@ -320,7 +319,8 @@ pio.templates["draft"] = go.layout.Template(
320
319
)
321
320
pio.templates.default = " draft"
322
321
323
- go.Figure().show()
322
+ fig = go.Figure()
323
+ fig.show()
324
324
```
325
325
326
326
### Combining themes
@@ -352,7 +352,8 @@ pio.templates["draft"] = go.layout.Template(
352
352
)
353
353
pio.templates.default = " plotly+draft"
354
354
355
- go.Figure().show()
355
+ fig = go.Figure()
356
+ fig.show()
356
357
```
357
358
358
359
Combining themes is also supported by plotly express
@@ -381,12 +382,11 @@ pio.templates.default = "plotly+draft"
381
382
382
383
gapminder = px.data.gapminder()
383
384
gapminder_2007 = gapminder.query(" year==2007" )
384
-
385
- px.scatter(gapminder_2007,
386
- x = " gdpPercap" , y = " lifeExp" , size = " pop" , color = " continent" ,
387
- log_x = True , size_max = 60 ,
388
- title = " Gapminder 2007: current default theme"
389
- ).show()
385
+ fig = px.scatter(gapminder_2007,
386
+ x = " gdpPercap" , y = " lifeExp" , size = " pop" , color = " continent" ,
387
+ log_x = True , size_max = 60 ,
388
+ title = " Gapminder 2007: current default theme" )
389
+ fig.show()
390
390
```
391
391
392
392
<!-- #region -->
0 commit comments