You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notebooks/creating-updating-figures.md
+67-68
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,7 @@ jupyter:
27
27
---
28
28
29
29
# Representing Figures
30
+
30
31
## Figures as dictionaries
31
32
The goal of plotly.py is to provide a pleasant Python interface for creating figure specifications for display in the Plotly.js JavaScript library. In Plotly.js, a figure is specified by a declarative JSON data structure, and so the ultimate responsibility of plotly.py is to produce Python dictionaries that can be serialized into a JSON data structure that represents a valid figure.
32
33
@@ -43,9 +44,9 @@ fig = {
43
44
pio.show(fig)
44
45
```
45
46
46
-
The value of the top-level `'data'` key is a list of trace specifications. Each trace specification has a special `'type'` key that indicates the trace type that is being defined (e.g. a `'bar'`, `'scatter'`, `'contour'`, etc.). The rest of the keys in the trace specification are used to configure the properties of the trace of this type.
47
+
The value of the top-level `"data"` key is a list of trace specifications. Each trace specification has a special `"type"` key that indicates the trace type that is being defined (e.g. a `"bar"`, `"scatter"`, `"contour"`, etc.). The rest of the keys in the trace specification are used to configure the properties of the trace of this type.
47
48
48
-
The value of the top-level `'layout'` key is a dictionary that specifies the properties of the figure's layout. In contrast to trace configuration options that apply to individual traces, the layout configuration options apply to the figure as a whole.
49
+
The value of the top-level `"layout"` key is a dictionary that specifies the properties of the figure's layout. In contrast to trace configuration options that apply to individual traces, the layout configuration options apply to the figure as a whole.
49
50
50
51
The [*Full Reference*](https://plot.ly/python/reference/) page contains descriptions of all of the supported trace and layout options.
51
52
@@ -98,7 +99,7 @@ As demonstrated above, you can build a complete figure by passing trace and layo
98
99
import plotly.graph_objects as go
99
100
fig = go.Figure(
100
101
data=[go.Bar(x=[1, 2, 3], y=[1, 3, 2])],
101
-
layout={"title": {"text": "A Bar Chart"}}
102
+
layout=dict(title=dict(text="A Bar Chart"))
102
103
)
103
104
fig.show()
104
105
```
@@ -133,7 +134,7 @@ The `plotly.subplots.make_subplots` function produces a graph object figure that
To make it easier to work with nested properties graph object constructors, and many graph object methods, support magic underscore notation. This allows you to reference nested properties by joining together multiple nested property names with underscores. For example, specifying the figure title in the figure constructor *without* magic underscore notation requires setting the `layout` argument to `{"title": {"text": "A Chart"}}`.
212
+
To make it easier to work with nested properties graph object constructors, and many graph object methods, support magic underscore notation. This allows you to reference nested properties by joining together multiple nested property names with underscores. For example, specifying the figure title in the figure constructor *without* magic underscore notation requires setting the `layout` argument to `dict(title=dict(text="A Chart"))`.
212
213
213
214
```python
214
215
import plotly.graph_objects as go
215
-
fig = go.Figure(layout={"title": {"text": "A Chart"}})
Note that the following `update_layout` operations are equivalent:
261
262
262
263
```python
263
-
fig.update_layout({"title": {"text": "A Bar Chart",
264
-
"font": {"size": 30}}})
265
-
266
-
fig.update_layout(title={"text": "A Bar Chart",
267
-
"font": {"size": 30}})
268
-
269
-
fig.update_layout(title=dict(text="A Bar Chart",
270
-
font=dict(size=30)))
271
-
272
264
fig.update_layout(title_text="A Bar Chart",
273
-
title_font={"size": 30})
265
+
title_font_size=30)
274
266
275
267
fig.update_layout(title_text="A Bar Chart",
276
-
title_font_size=30);
268
+
title_font=dict(size=30))
269
+
270
+
271
+
fig.update_layout(title=dict(text="A Bar Chart"),
272
+
font=dict(size=30))
273
+
274
+
fig.update_layout({"title": {"text": "A Bar Chart",
275
+
"font": {"size": 30}}})
277
276
278
277
fig.update_layout(
279
278
title=go.layout.Title(text="A Bar Chart",
@@ -288,20 +287,20 @@ Graph object figures support an `update_traces` method that may be used to updat
288
287
from plotly.subplots import make_subplots
289
288
fig = make_subplots(rows=1, cols=2)
290
289
291
-
fig.add_scatter(y=[4, 2, 3.5], mode='markers',
292
-
marker={'size': 20, 'color': 'LightSeaGreen'},
290
+
fig.add_scatter(y=[4, 2, 3.5], mode="markers",
291
+
marker=dict(size=20, color="LightSeaGreen"),
293
292
name="a", row=1, col=1)
294
293
295
294
fig.add_bar(y=[2, 1, 3],
296
-
marker={'color': 'MediumPurple'},
295
+
marker=dict(color="MediumPurple"),
297
296
name="b", row=1, col=1)
298
297
299
-
fig.add_scatter(y=[2, 3.5, 4], mode='markers',
300
-
marker={'size': 20, 'color': 'MediumPurple'},
298
+
fig.add_scatter(y=[2, 3.5, 4], mode="markers",
299
+
marker=dict(size=20, color="MediumPurple"),
301
300
name="c", row=1, col=2)
302
301
303
302
fig.add_bar(y=[1, 3, 2],
304
-
marker={'color': 'LightSeaGreen'},
303
+
marker=dict(color="LightSeaGreen"),
305
304
name="d", row=1, col=2)
306
305
307
306
fig.show()
@@ -313,23 +312,23 @@ Note that both `scatter` and `bar` traces have a `marker.color` property to cont
313
312
from plotly.subplots import make_subplots
314
313
fig = make_subplots(rows=1, cols=2)
315
314
316
-
fig.add_scatter(y=[4, 2, 3.5], mode='markers',
317
-
marker={'size': 20, 'color': 'LightSeaGreen'},
315
+
fig.add_scatter(y=[4, 2, 3.5], mode="markers",
316
+
marker=dict(size=20, color="LightSeaGreen"),
318
317
name="a", row=1, col=1)
319
318
320
319
fig.add_bar(y=[2, 1, 3],
321
-
marker={'color': 'MediumPurple'},
320
+
marker=dict(color="MediumPurple"),
322
321
name="b", row=1, col=1)
323
322
324
-
fig.add_scatter(y=[2, 3.5, 4], mode='markers',
325
-
marker={'size': 20, 'color': 'MediumPurple'},
323
+
fig.add_scatter(y=[2, 3.5, 4], mode="markers",
324
+
marker=dict(size=20, color="MediumPurple"),
326
325
name="c", row=1, col=2)
327
326
328
327
fig.add_bar(y=[1, 3, 2],
329
-
marker={'color': 'LightSeaGreen'},
328
+
marker=dict(color="LightSeaGreen"),
330
329
name="d", row=1, col=2)
331
330
332
-
fig.update_traces(marker={'color': 'RoyalBlue'})
331
+
fig.update_traces(marker=dict(color="RoyalBlue"))
333
332
334
333
fig.show()
335
334
```
@@ -340,52 +339,52 @@ The `update_traces` method supports a `selector` argument to control which trace
340
339
from plotly.subplots import make_subplots
341
340
fig = make_subplots(rows=1, cols=2)
342
341
343
-
fig.add_scatter(y=[4, 2, 3.5], mode='markers',
344
-
marker={'size': 20, 'color': 'LightSeaGreen'},
342
+
fig.add_scatter(y=[4, 2, 3.5], mode="markers",
343
+
marker=dict(size=20, color="LightSeaGreen"),
345
344
name="a", row=1, col=1)
346
345
347
346
fig.add_bar(y=[2, 1, 3],
348
-
marker={'color': 'MediumPurple'},
347
+
marker=dict(color="MediumPurple"),
349
348
name="b", row=1, col=1)
350
349
351
-
fig.add_scatter(y=[2, 3.5, 4], mode='markers',
352
-
marker={'size': 20, 'color': 'MediumPurple'},
350
+
fig.add_scatter(y=[2, 3.5, 4], mode="markers",
351
+
marker=dict(size=20, color="MediumPurple"),
353
352
name="c", row=1, col=2)
354
353
355
354
fig.add_bar(y=[1, 3, 2],
356
-
marker={'color': 'LightSeaGreen'},
355
+
marker=dict(color="LightSeaGreen"),
357
356
name="d", row=1, col=2)
358
357
359
-
fig.update_traces(marker={'color': 'RoyalBlue'},
360
-
selector={'type': 'bar'})
358
+
fig.update_traces(marker=dict(color="RoyalBlue"),
359
+
selector=dict(type="bar"))
361
360
362
361
fig.show()
363
362
```
364
363
365
-
Magic underscore notation can be used in the selector to match nested properties. Here is an example of updating the color of all traces that were formally colored `'MediumPurple'`.
364
+
Magic underscore notation can be used in the selector to match nested properties. Here is an example of updating the color of all traces that were formally colored `"MediumPurple"`.
366
365
367
366
```python
368
367
from plotly.subplots import make_subplots
369
368
fig = make_subplots(rows=1, cols=2)
370
369
371
-
fig.add_scatter(y=[4, 2, 3.5], mode='markers',
372
-
marker={'size': 20, 'color': 'LightSeaGreen'},
370
+
fig.add_scatter(y=[4, 2, 3.5], mode="markers",
371
+
marker=dict(size=20, color="LightSeaGreen"),
373
372
name="a", row=1, col=1)
374
373
375
374
fig.add_bar(y=[2, 1, 3],
376
-
marker={'color': 'MediumPurple'},
375
+
marker=dict(color="MediumPurple"),
377
376
name="b", row=1, col=1)
378
377
379
-
fig.add_scatter(y=[2, 3.5, 4], mode='markers',
380
-
marker={'size': 20, 'color': 'MediumPurple'},
378
+
fig.add_scatter(y=[2, 3.5, 4], mode="markers",
379
+
marker=dict(size=20, color="MediumPurple"),
381
380
name="c", row=1, col=2)
382
381
383
382
fig.add_bar(y=[1, 3, 2],
384
-
marker={'color': 'LightSeaGreen'},
383
+
marker=dict(color="LightSeaGreen"),
385
384
name="d", row=1, col=2)
386
385
387
-
fig.update_traces(marker_color='RoyalBlue',
388
-
selector={'marker_color': 'MediumPurple'})
386
+
fig.update_traces(marker_color="RoyalBlue",
387
+
selector=dict(marker_color="MediumPurple"))
389
388
390
389
fig.show()
391
390
```
@@ -396,23 +395,23 @@ For figures with subplots, the `update_traces` method also supports `row` and `c
396
395
from plotly.subplots import make_subplots
397
396
fig = make_subplots(rows=1, cols=2)
398
397
399
-
fig.add_scatter(y=[4, 2, 3.5], mode='markers',
400
-
marker={'size': 20, 'color': 'LightSeaGreen'},
398
+
fig.add_scatter(y=[4, 2, 3.5], mode="markers",
399
+
marker=dict(size=20, color="LightSeaGreen"),
401
400
name="a", row=1, col=1)
402
401
403
402
fig.add_bar(y=[2, 1, 3],
404
-
marker={'color': 'MediumPurple'},
403
+
marker=dict(color="MediumPurple"),
405
404
name="b", row=1, col=1)
406
405
407
-
fig.add_scatter(y=[2, 3.5, 4], mode='markers',
408
-
marker={'size': 20, 'color': 'MediumPurple'},
406
+
fig.add_scatter(y=[2, 3.5, 4], mode="markers",
407
+
marker=dict(size=20, color="MediumPurple"),
409
408
name="c", row=1, col=2)
410
409
411
410
fig.add_bar(y=[1, 3, 2],
412
-
marker={'color': 'LightSeaGreen'},
411
+
marker=dict(color="LightSeaGreen"),
413
412
name="d", row=1, col=2)
414
413
415
-
fig.update_traces(marker={'color': 'RoyalBlue'},
414
+
fig.update_traces(marker=dict(color="RoyalBlue"),
416
415
col=2)
417
416
418
417
fig.show()
@@ -424,10 +423,10 @@ The `update_traces` method can also be used on figures produced by figure factor
0 commit comments