Skip to content

Commit e5bafb7

Browse files
authored
document new methods add_shape add_annotation (#171)
1 parent 10b0793 commit e5bafb7

File tree

2 files changed

+89
-114
lines changed

2 files changed

+89
-114
lines changed

python/shapes.md

Lines changed: 50 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jupyter:
66
extension: .md
77
format_name: markdown
88
format_version: '1.1'
9-
jupytext_version: 1.1.6
9+
jupytext_version: 1.1.1
1010
kernelspec:
1111
display_name: Python 3
1212
language: python
@@ -55,8 +55,7 @@ fig.update_xaxes(range=[0, 7])
5555
fig.update_yaxes(range=[0, 2.5])
5656

5757
# Add shapes
58-
fig.update_layout(
59-
shapes=[
58+
fig.add_shape(
6059
# Line Vertical
6160
go.layout.Shape(
6261
type="line",
@@ -68,7 +67,8 @@ fig.update_layout(
6867
color="RoyalBlue",
6968
width=3
7069
)
71-
),
70+
))
71+
fig.add_shape(
7272
# Line Horizontal
7373
go.layout.Shape(
7474
type="line",
@@ -81,7 +81,8 @@ fig.update_layout(
8181
width=4,
8282
dash="dashdot",
8383
),
84-
),
84+
))
85+
fig.add_shape(
8586
# Line Diagonal
8687
go.layout.Shape(
8788
type="line",
@@ -93,11 +94,9 @@ fig.update_layout(
9394
color="MediumPurple",
9495
width=4,
9596
dash="dot",
96-
),
97-
),
98-
]
99-
)
100-
97+
)
98+
))
99+
fig.update_shapes(dict(xref='x', yref='y'))
101100
fig.show()
102101
```
103102

@@ -122,8 +121,7 @@ fig.update_xaxes(range=[0, 8])
122121
fig.update_yaxes(range=[0, 2])
123122

124123
# Add shapes
125-
fig.update_layout(
126-
shapes=[
124+
fig.add_shape(
127125
# Line reference to the axes
128126
go.layout.Shape(
129127
type="line",
@@ -137,7 +135,8 @@ fig.update_layout(
137135
color="LightSeaGreen",
138136
width=3,
139137
),
140-
),
138+
))
139+
fig.add_shape(
141140
# Line reference to the plot
142141
go.layout.Shape(
143142
type="line",
@@ -152,7 +151,6 @@ fig.update_layout(
152151
width=3,
153152
),
154153
),
155-
]
156154
)
157155

158156
fig.show()
@@ -165,7 +163,7 @@ import plotly.graph_objects as go
165163

166164
import numpy as np
167165

168-
# Generate date
166+
# Generate data
169167
x0 = np.linspace(1, 3, 200)
170168
y0 = x0 * np.sin(np.power(x0, 2)) + 1
171169

@@ -183,47 +181,38 @@ fig.update_layout(
183181
)
184182

185183
# Add tangent line shapes
186-
fig.update_layout(
187-
shapes=[
184+
fig.add_shape(
188185
go.layout.Shape(
189186
type="line",
190187
x0=1,
191188
y0=2.30756,
192189
x1=1.75,
193190
y1=2.30756,
194-
opacity=0.7,
195-
line=dict(
196-
color="Crimson",
197-
width=2.5,
198-
),
199-
),
191+
))
192+
fig.add_shape(
200193
go.layout.Shape(
201194
type="line",
202195
x0=2.5,
203196
y0=3.80796,
204197
x1=3.05,
205198
y1=3.80796,
206-
opacity=0.7,
207-
line=dict(
208-
color="Crimson",
209-
width=2.5,
210-
),
211-
),
199+
))
200+
fig.add_shape(
212201
go.layout.Shape(
213202
type="line",
214203
x0=1.90,
215204
y0=-1.1827,
216205
x1=2.50,
217206
y1=-1.1827,
218-
opacity=0.7,
219-
line=dict(
220-
color="Crimson",
221-
width=2.5,
222-
),
223-
),
224-
]
225-
)
226-
207+
))
208+
fig.update_shapes(dict(
209+
xref="x",
210+
yref="y",
211+
opacity=0.7,
212+
line=dict(
213+
color="Crimson",
214+
width=2.5,
215+
)))
227216
fig.show()
228217
```
229218

@@ -246,8 +235,7 @@ fig.update_xaxes(range=[0, 7], showgrid=False)
246235
fig.update_yaxes(range=[0, 3.5])
247236

248237
# Add shapes
249-
fig.update_layout(
250-
shapes=[
238+
fig.add_shape(
251239
# unfilled Rectangle
252240
go.layout.Shape(
253241
type="rect",
@@ -258,7 +246,8 @@ fig.update_layout(
258246
line=dict(
259247
color="RoyalBlue",
260248
),
261-
),
249+
))
250+
fig.add_shape(
262251
# filled Rectangle
263252
go.layout.Shape(
264253
type="rect",
@@ -271,10 +260,8 @@ fig.update_layout(
271260
width=2,
272261
),
273262
fillcolor="LightSkyBlue",
274-
),
275-
]
276-
)
277-
263+
))
264+
fig.update_shapes(dict(xref='x', yref='y'))
278265
fig.show()
279266
```
280267

@@ -299,8 +286,7 @@ fig.update_xaxes(range=[0, 4], showgrid=False)
299286
fig.update_yaxes(range=[0, 4])
300287

301288
# Add shapes
302-
fig.update_layout(
303-
shapes=[
289+
fig.add_shape(
304290
# Rectangle reference to the axes
305291
go.layout.Shape(
306292
type="rect",
@@ -315,7 +301,8 @@ fig.update_layout(
315301
width=3,
316302
),
317303
fillcolor="LightSkyBlue",
318-
),
304+
))
305+
fig.add_shape(
319306
# Rectangle reference to the plot
320307
go.layout.Shape(
321308
type="rect",
@@ -330,9 +317,7 @@ fig.update_layout(
330317
width=3,
331318
),
332319
fillcolor="PaleTurquoise",
333-
),
334-
]
335-
)
320+
))
336321

337322
fig.show()
338323
```
@@ -592,37 +577,32 @@ fig.update_yaxes(
592577
)
593578

594579
# Add circles
595-
fig.update_layout(
596-
shapes=[
580+
fig.add_shape(
597581
go.layout.Shape(
598-
opacity=0.3,
599-
xref="x",
600-
yref="y",
582+
type="circle",
601583
fillcolor="blue",
602584
x0=0,
603585
y0=0,
604586
x1=2,
605587
y1=2,
606-
type="circle",
607-
line_color="blue",
608-
layer="below"
609-
),
588+
line_color="blue"
589+
))
590+
fig.add_shape(
610591
go.layout.Shape(
611-
opacity=0.3,
612-
xref="x",
613-
yref="y",
592+
type="circle",
614593
fillcolor="gray",
615594
x0=1.5,
616595
y0=0,
617596
x1=3.5,
618597
y1=2,
619-
type="circle",
620-
line_color="gray",
621-
layer="below"
622-
)
623-
],
624-
)
625-
598+
line_color="gray"
599+
))
600+
fig.update_shapes(dict(
601+
opacity=0.3,
602+
xref="x",
603+
yref="y",
604+
layer="below"
605+
))
626606
# Update figure dimensions
627607
fig.update_layout(
628608
margin=dict(

python/text-and-annotations.md

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ fig.show()
111111

112112
### Simple Annotation
113113

114+
Annotations can be added to a figure using `fig.update_layout(annotations=[...])` or `fig.add_annotation`.
115+
114116
```python
115117
import plotly.graph_objects as go
116118

@@ -164,33 +166,28 @@ fig.add_trace(go.Scatter(
164166
y=[0, 4, 5, 1, 2, 2, 3, 4, 2]
165167
))
166168

167-
fig.update_layout(
168-
showlegend=False,
169-
annotations=[
170-
go.layout.Annotation(
169+
fig.add_annotation(
170+
go.layout.Annotation(
171171
x=2,
172172
y=5,
173-
xref="x",
174-
yref="y",
175-
text="dict Text",
176-
showarrow=True,
177-
arrowhead=7,
178-
ax=0,
179-
ay=-40
180-
),
181-
go.layout.Annotation(
173+
text="dict Text")
174+
)
175+
fig.add_annotation(
176+
go.layout.Annotation(
182177
x=4,
183178
y=4,
179+
text="dict Text 2")
180+
)
181+
fig.update_annotations(dict(
184182
xref="x",
185183
yref="y",
186-
text="dict Text 2",
187184
showarrow=True,
188185
arrowhead=7,
189186
ax=0,
190187
ay=-40
191-
)
192-
]
193-
)
188+
))
189+
190+
fig.update_layout(showlegend=False)
194191

195192
fig.show()
196193
```
@@ -347,37 +344,35 @@ fig.add_trace(go.Scatter(
347344
y=[0, 4, 5, 1, 2, 2, 3, 4, 2]
348345
))
349346

350-
fig.update_layout(
351-
showlegend=False,
352-
annotations=[
353-
go.layout.Annotation(
354-
x=2,
355-
y=5,
356-
xref="x",
357-
yref="y",
358-
text="max=5",
359-
showarrow=True,
360-
font=dict(
361-
family="Courier New, monospace",
362-
size=16,
363-
color="#ffffff"
347+
fig.add_annotation(
348+
go.layout.Annotation(
349+
x=2,
350+
y=5,
351+
xref="x",
352+
yref="y",
353+
text="max=5",
354+
showarrow=True,
355+
font=dict(
356+
family="Courier New, monospace",
357+
size=16,
358+
color="#ffffff"
364359
),
365-
align="center",
366-
arrowhead=2,
367-
arrowsize=1,
368-
arrowwidth=2,
369-
arrowcolor="#636363",
370-
ax=20,
371-
ay=-30,
372-
bordercolor="#c7c7c7",
373-
borderwidth=2,
374-
borderpad=4,
375-
bgcolor="#ff7f0e",
376-
opacity=0.8
360+
align="center",
361+
arrowhead=2,
362+
arrowsize=1,
363+
arrowwidth=2,
364+
arrowcolor="#636363",
365+
ax=20,
366+
ay=-30,
367+
bordercolor="#c7c7c7",
368+
borderwidth=2,
369+
borderpad=4,
370+
bgcolor="#ff7f0e",
371+
opacity=0.8
377372
)
378-
]
379373
)
380374

375+
fig.update_layout(showlegend=False)
381376
fig.show()
382377
```
383378

0 commit comments

Comments
 (0)