Skip to content

Commit 51ed6f4

Browse files
committed
Standardize on double quotes and dict constructor for small dictionaries
1 parent 61000ba commit 51ed6f4

File tree

1 file changed

+67
-68
lines changed

1 file changed

+67
-68
lines changed

notebooks/creating-updating-figures.md

+67-68
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jupyter:
2727
---
2828

2929
# Representing Figures
30+
3031
## Figures as dictionaries
3132
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.
3233

@@ -43,9 +44,9 @@ fig = {
4344
pio.show(fig)
4445
```
4546

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.
4748

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.
4950

5051
The [*Full Reference*](https://plot.ly/python/reference/) page contains descriptions of all of the supported trace and layout options.
5152

@@ -98,7 +99,7 @@ As demonstrated above, you can build a complete figure by passing trace and layo
9899
import plotly.graph_objects as go
99100
fig = go.Figure(
100101
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"))
102103
)
103104
fig.show()
104105
```
@@ -133,7 +134,7 @@ The `plotly.subplots.make_subplots` function produces a graph object figure that
133134
```python
134135
from plotly.subplots import make_subplots
135136
fig = make_subplots(rows=1, cols=2)
136-
fig.add_trace(go.Scatter(y=[4, 2, 1], mode='lines'), row=1, col=1)
137+
fig.add_trace(go.Scatter(y=[4, 2, 1], mode="lines"), row=1, col=1)
137138
fig.add_trace(go.Bar(y=[2, 1, 3]), row=1, col=2)
138139
fig.show()
139140
```
@@ -161,8 +162,8 @@ fig.add_trace(
161162
go.Scatter(
162163
x=[2, 4],
163164
y=[4, 8],
164-
mode='lines',
165-
line=go.scatter.Line(color='gray'),
165+
mode="lines",
166+
line=go.scatter.Line(color="gray"),
166167
showlegend=False)
167168
)
168169
fig.show()
@@ -174,7 +175,7 @@ If a figure was created using `plotly.subplots.make_subplots`, then the `row` an
174175
```python
175176
from plotly.subplots import make_subplots
176177
fig = make_subplots(rows=1, cols=2)
177-
fig.add_trace(go.Scatter(y=[4, 2, 1], mode='lines'), row=1, col=1)
178+
fig.add_trace(go.Scatter(y=[4, 2, 1], mode="lines"), row=1, col=1)
178179
fig.add_trace(go.Bar(y=[2, 1, 3]), row=1, col=2)
179180
fig.show()
180181
```
@@ -187,8 +188,8 @@ iris = px.data.iris()
187188
fig = px.scatter(iris, x="sepal_width", y="sepal_length", color="species", facet_col="species")
188189
reference_line = go.Scatter(x=[2, 4],
189190
y=[4, 8],
190-
mode='lines',
191-
line=go.scatter.Line(color='gray'),
191+
mode="lines",
192+
line=go.scatter.Line(color="gray"),
192193
showlegend=False)
193194
fig.add_trace(reference_line, row=1, col=1)
194195
fig.add_trace(reference_line, row=1, col=2)
@@ -202,17 +203,17 @@ As an alternative to the `add_trace` method, graph object figures have a family
202203
```python
203204
from plotly.subplots import make_subplots
204205
fig = make_subplots(rows=1, cols=2)
205-
fig.add_scatter(y=[4, 2, 1], mode='lines', row=1, col=1)
206+
fig.add_scatter(y=[4, 2, 1], mode="lines", row=1, col=1)
206207
fig.add_bar(y=[2, 1, 3], row=1, col=2)
207208
fig.show()
208209
```
209210

210211
### Magic underscore notation
211-
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"))`.
212213

213214
```python
214215
import plotly.graph_objects as go
215-
fig = go.Figure(layout={"title": {"text": "A Chart"}})
216+
fig = go.Figure(layout=dict(title=dict(text="A Chart")))
216217
fig.show()
217218
```
218219

@@ -232,7 +233,7 @@ Trace and layout properties can be updated using property assignment syntax. He
232233
```python
233234
import plotly.graph_objects as go
234235
fig = go.Figure(data=go.Bar(x=[1, 2, 3], y=[1, 3, 2]))
235-
fig.layout.title.text = 'A Bar Chart'
236+
fig.layout.title.text = "A Bar Chart"
236237
fig.show()
237238
```
238239

@@ -242,7 +243,7 @@ And here is an example of updating the bar outline using property assignment
242243
import plotly.graph_objects as go
243244
fig = go.Figure(data=go.Bar(x=[1, 2, 3], y=[1, 3, 2]))
244245
fig.data[0].marker.line.width = 4
245-
fig.data[0].marker.line.color = 'black'
246+
fig.data[0].marker.line.color = "black"
246247
fig.show()
247248
```
248249

@@ -260,20 +261,18 @@ fig.show()
260261
Note that the following `update_layout` operations are equivalent:
261262

262263
```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-
272264
fig.update_layout(title_text="A Bar Chart",
273-
title_font={"size": 30})
265+
title_font_size=30)
274266

275267
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}}})
277276

278277
fig.update_layout(
279278
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
288287
from plotly.subplots import make_subplots
289288
fig = make_subplots(rows=1, cols=2)
290289

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"),
293292
name="a", row=1, col=1)
294293

295294
fig.add_bar(y=[2, 1, 3],
296-
marker={'color': 'MediumPurple'},
295+
marker=dict(color="MediumPurple"),
297296
name="b", row=1, col=1)
298297

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"),
301300
name="c", row=1, col=2)
302301

303302
fig.add_bar(y=[1, 3, 2],
304-
marker={'color': 'LightSeaGreen'},
303+
marker=dict(color="LightSeaGreen"),
305304
name="d", row=1, col=2)
306305

307306
fig.show()
@@ -313,23 +312,23 @@ Note that both `scatter` and `bar` traces have a `marker.color` property to cont
313312
from plotly.subplots import make_subplots
314313
fig = make_subplots(rows=1, cols=2)
315314

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"),
318317
name="a", row=1, col=1)
319318

320319
fig.add_bar(y=[2, 1, 3],
321-
marker={'color': 'MediumPurple'},
320+
marker=dict(color="MediumPurple"),
322321
name="b", row=1, col=1)
323322

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"),
326325
name="c", row=1, col=2)
327326

328327
fig.add_bar(y=[1, 3, 2],
329-
marker={'color': 'LightSeaGreen'},
328+
marker=dict(color="LightSeaGreen"),
330329
name="d", row=1, col=2)
331330

332-
fig.update_traces(marker={'color': 'RoyalBlue'})
331+
fig.update_traces(marker=dict(color="RoyalBlue"))
333332

334333
fig.show()
335334
```
@@ -340,52 +339,52 @@ The `update_traces` method supports a `selector` argument to control which trace
340339
from plotly.subplots import make_subplots
341340
fig = make_subplots(rows=1, cols=2)
342341

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"),
345344
name="a", row=1, col=1)
346345

347346
fig.add_bar(y=[2, 1, 3],
348-
marker={'color': 'MediumPurple'},
347+
marker=dict(color="MediumPurple"),
349348
name="b", row=1, col=1)
350349

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"),
353352
name="c", row=1, col=2)
354353

355354
fig.add_bar(y=[1, 3, 2],
356-
marker={'color': 'LightSeaGreen'},
355+
marker=dict(color="LightSeaGreen"),
357356
name="d", row=1, col=2)
358357

359-
fig.update_traces(marker={'color': 'RoyalBlue'},
360-
selector={'type': 'bar'})
358+
fig.update_traces(marker=dict(color="RoyalBlue"),
359+
selector=dict(type="bar"))
361360

362361
fig.show()
363362
```
364363

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"`.
366365

367366
```python
368367
from plotly.subplots import make_subplots
369368
fig = make_subplots(rows=1, cols=2)
370369

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"),
373372
name="a", row=1, col=1)
374373

375374
fig.add_bar(y=[2, 1, 3],
376-
marker={'color': 'MediumPurple'},
375+
marker=dict(color="MediumPurple"),
377376
name="b", row=1, col=1)
378377

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"),
381380
name="c", row=1, col=2)
382381

383382
fig.add_bar(y=[1, 3, 2],
384-
marker={'color': 'LightSeaGreen'},
383+
marker=dict(color="LightSeaGreen"),
385384
name="d", row=1, col=2)
386385

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"))
389388

390389
fig.show()
391390
```
@@ -396,23 +395,23 @@ For figures with subplots, the `update_traces` method also supports `row` and `c
396395
from plotly.subplots import make_subplots
397396
fig = make_subplots(rows=1, cols=2)
398397

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"),
401400
name="a", row=1, col=1)
402401

403402
fig.add_bar(y=[2, 1, 3],
404-
marker={'color': 'MediumPurple'},
403+
marker=dict(color="MediumPurple"),
405404
name="b", row=1, col=1)
406405

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"),
409408
name="c", row=1, col=2)
410409

411410
fig.add_bar(y=[1, 3, 2],
412-
marker={'color': 'LightSeaGreen'},
411+
marker=dict(color="LightSeaGreen"),
413412
name="d", row=1, col=2)
414413

415-
fig.update_traces(marker={'color': 'RoyalBlue'},
414+
fig.update_traces(marker=dict(color="RoyalBlue"),
416415
col=2)
417416

418417
fig.show()
@@ -424,10 +423,10 @@ The `update_traces` method can also be used on figures produced by figure factor
424423
import pandas as pd
425424
import plotly.express as px
426425
iris = px.data.iris()
427-
fig = px.scatter(iris, x="sepal_width", y="sepal_length", color="species", facet_col="species", trendline='ols')
426+
fig = px.scatter(iris, x="sepal_width", y="sepal_length", color="species", facet_col="species", trendline="ols")
428427
fig.update_traces(
429-
line={'dash': 'dot', 'width': 4},
430-
selector={'type': 'scatter', 'mode': 'lines'})
428+
line=dict(dash="dot", width=4),
429+
selector=dict(type="scatter", mode="lines"))
431430
fig.show()
432431
```
433432

@@ -445,7 +444,7 @@ iris = px.data.iris()
445444
fig = px.scatter(iris, x="sepal_width", y="sepal_length", color="species")
446445

447446
fig.for_each_trace(
448-
lambda trace: trace.update(name=trace.name.replace('=', ': ')),
447+
lambda trace: trace.update(name=trace.name.replace("=", ": ")),
449448
)
450449

451450
fig.show()
@@ -479,7 +478,7 @@ iris = px.data.iris()
479478
.update_layout(title_font_size=24)
480479
.update_xaxes(showgrid=False)
481480
.update_traces(
482-
line={'dash': 'dot', 'width': 4},
483-
selector={'type': 'scatter', 'mode': 'lines'})
481+
line=dict(dash="dot", width=4),
482+
selector=dict(type="scatter", mode="lines"))
484483
).show()
485484
```

0 commit comments

Comments
 (0)