Skip to content

Commit fcd2a79

Browse files
committed
document new methods add_shape add_annotation
1 parent 319b91d commit fcd2a79

File tree

2 files changed

+91
-118
lines changed

2 files changed

+91
-118
lines changed

python/shapes.md

Lines changed: 50 additions & 71 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
@@ -33,7 +33,6 @@ jupyter:
3333
order: 32
3434
permalink: python/shapes/
3535
thumbnail: thumbnail/shape.jpg
36-
3736
v4upgrade: true
3837
---
3938

@@ -59,8 +58,7 @@ fig.update_xaxes(range=[0, 7])
5958
fig.update_yaxes(range=[0, 2.5])
6059

6160
# Add shapes
62-
fig.update_layout(
63-
shapes=[
61+
fig.add_shape(
6462
# Line Vertical
6563
go.layout.Shape(
6664
type="line",
@@ -72,7 +70,8 @@ fig.update_layout(
7270
color="RoyalBlue",
7371
width=3
7472
)
75-
),
73+
))
74+
fig.add_shape(
7675
# Line Horizontal
7776
go.layout.Shape(
7877
type="line",
@@ -85,7 +84,8 @@ fig.update_layout(
8584
width=4,
8685
dash="dashdot",
8786
),
88-
),
87+
))
88+
fig.add_shape(
8989
# Line Diagonal
9090
go.layout.Shape(
9191
type="line",
@@ -97,11 +97,9 @@ fig.update_layout(
9797
color="MediumPurple",
9898
width=4,
9999
dash="dot",
100-
),
101-
),
102-
]
103-
)
104-
100+
)
101+
))
102+
fig.update_shapes(dict(xref='x', yref='y'))
105103
fig.show()
106104
```
107105

@@ -126,8 +124,7 @@ fig.update_xaxes(range=[0, 8])
126124
fig.update_yaxes(range=[0, 2])
127125

128126
# Add shapes
129-
fig.update_layout(
130-
shapes=[
127+
fig.add_shape(
131128
# Line reference to the axes
132129
go.layout.Shape(
133130
type="line",
@@ -141,7 +138,8 @@ fig.update_layout(
141138
color="LightSeaGreen",
142139
width=3,
143140
),
144-
),
141+
))
142+
fig.add_shape(
145143
# Line reference to the plot
146144
go.layout.Shape(
147145
type="line",
@@ -156,7 +154,6 @@ fig.update_layout(
156154
width=3,
157155
),
158156
),
159-
]
160157
)
161158

162159
fig.show()
@@ -169,7 +166,7 @@ import plotly.graph_objects as go
169166

170167
import numpy as np
171168

172-
# Generate date
169+
# Generate data
173170
x0 = np.linspace(1, 3, 200)
174171
y0 = x0 * np.sin(np.power(x0, 2)) + 1
175172

@@ -187,47 +184,38 @@ fig.update_layout(
187184
)
188185

189186
# Add tangent line shapes
190-
fig.update_layout(
191-
shapes=[
187+
fig.add_shape(
192188
go.layout.Shape(
193189
type="line",
194190
x0=1,
195191
y0=2.30756,
196192
x1=1.75,
197193
y1=2.30756,
198-
opacity=0.7,
199-
line=dict(
200-
color="Crimson",
201-
width=2.5,
202-
),
203-
),
194+
))
195+
fig.add_shape(
204196
go.layout.Shape(
205197
type="line",
206198
x0=2.5,
207199
y0=3.80796,
208200
x1=3.05,
209201
y1=3.80796,
210-
opacity=0.7,
211-
line=dict(
212-
color="Crimson",
213-
width=2.5,
214-
),
215-
),
202+
))
203+
fig.add_shape(
216204
go.layout.Shape(
217205
type="line",
218206
x0=1.90,
219207
y0=-1.1827,
220208
x1=2.50,
221209
y1=-1.1827,
222-
opacity=0.7,
223-
line=dict(
224-
color="Crimson",
225-
width=2.5,
226-
),
227-
),
228-
]
229-
)
230-
210+
))
211+
fig.update_shapes(dict(
212+
xref="x",
213+
yref="y",
214+
opacity=0.7,
215+
line=dict(
216+
color="Crimson",
217+
width=2.5,
218+
)))
231219
fig.show()
232220
```
233221

@@ -250,8 +238,7 @@ fig.update_xaxes(range=[0, 7], showgrid=False)
250238
fig.update_yaxes(range=[0, 3.5])
251239

252240
# Add shapes
253-
fig.update_layout(
254-
shapes=[
241+
fig.add_shape(
255242
# unfilled Rectangle
256243
go.layout.Shape(
257244
type="rect",
@@ -262,7 +249,8 @@ fig.update_layout(
262249
line=dict(
263250
color="RoyalBlue",
264251
),
265-
),
252+
))
253+
fig.add_shape(
266254
# filled Rectangle
267255
go.layout.Shape(
268256
type="rect",
@@ -275,10 +263,8 @@ fig.update_layout(
275263
width=2,
276264
),
277265
fillcolor="LightSkyBlue",
278-
),
279-
]
280-
)
281-
266+
))
267+
fig.update_shapes(dict(xref='x', yref='y'))
282268
fig.show()
283269
```
284270

@@ -303,8 +289,7 @@ fig.update_xaxes(range=[0, 4], showgrid=False)
303289
fig.update_yaxes(range=[0, 4])
304290

305291
# Add shapes
306-
fig.update_layout(
307-
shapes=[
292+
fig.add_shape(
308293
# Rectangle reference to the axes
309294
go.layout.Shape(
310295
type="rect",
@@ -319,7 +304,8 @@ fig.update_layout(
319304
width=3,
320305
),
321306
fillcolor="LightSkyBlue",
322-
),
307+
))
308+
fig.add_shape(
323309
# Rectangle reference to the plot
324310
go.layout.Shape(
325311
type="rect",
@@ -334,9 +320,7 @@ fig.update_layout(
334320
width=3,
335321
),
336322
fillcolor="PaleTurquoise",
337-
),
338-
]
339-
)
323+
))
340324

341325
fig.show()
342326
```
@@ -596,37 +580,32 @@ fig.update_yaxes(
596580
)
597581

598582
# Add circles
599-
fig.update_layout(
600-
shapes=[
583+
fig.add_shape(
601584
go.layout.Shape(
602-
opacity=0.3,
603-
xref="x",
604-
yref="y",
585+
type="circle",
605586
fillcolor="blue",
606587
x0=0,
607588
y0=0,
608589
x1=2,
609590
y1=2,
610-
type="circle",
611-
line_color="blue",
612-
layer="below"
613-
),
591+
line_color="blue"
592+
))
593+
fig.add_shape(
614594
go.layout.Shape(
615-
opacity=0.3,
616-
xref="x",
617-
yref="y",
595+
type="circle",
618596
fillcolor="gray",
619597
x0=1.5,
620598
y0=0,
621599
x1=3.5,
622600
y1=2,
623-
type="circle",
624-
line_color="gray",
625-
layer="below"
626-
)
627-
],
628-
)
629-
601+
line_color="gray"
602+
))
603+
fig.update_shapes(dict(
604+
opacity=0.3,
605+
xref="x",
606+
yref="y",
607+
layer="below"
608+
))
630609
# Update figure dimensions
631610
fig.update_layout(
632611
margin=dict(

0 commit comments

Comments
 (0)