Skip to content

Commit 319c671

Browse files
committed
graph_objects and update_layout
1 parent 6ee4ecd commit 319c671

File tree

4 files changed

+97
-77
lines changed

4 files changed

+97
-77
lines changed

notebooks/3d-axes.md

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ jupyter:
3939

4040
### Range of axes
4141

42-
The `go.Layout.scene` of a 3D figure can be configured through its `xaxis`, `yaxis` and `zaxis` parameters, in order to set the range, title, ticks, color etc. of the axes.
42+
3D figures have an attribute in `layout` called `scene`, which contains
43+
attributes such as `xaxis`, `yaxis` and `zaxis` parameters, in order to
44+
set the range, title, ticks, color etc. of the axes.
4345

4446
For creating 3D charts, see [this page](https://plot.ly/python/3d-charts/).
4547

4648
```python
47-
import plotly.graph_objs as go
49+
import plotly.graph_objects as go
4850
import numpy as np
4951

5052
N = 70
@@ -56,20 +58,20 @@ fig = go.Figure(data=[go.Mesh3d(x=(70*np.random.randn(N)),
5658
color='rgba(244,22,100,0.6)'
5759
)])
5860

59-
fig.update(layout=go.Layout(scene = dict(
60-
xaxis = dict(nticks=4, range=[-100,100],),
61+
fig.update_layout(scene = dict(
62+
xaxis = dict(nticks=4, range=[-100,100],),
6163
yaxis = dict(nticks=4, range=[-50,100],),
6264
zaxis = dict(nticks=4, range=[-100,100],),),
6365
width=700,
64-
margin=dict(r=20, l=10, b=10, t=10)))
66+
margin=dict(r=20, l=10, b=10, t=10))
6567

6668
fig.show()
6769
```
6870

6971
### Fixed Ratio Axes
7072

7173
```python
72-
import plotly.graph_objs as go
74+
import plotly.graph_objects as go
7375
from plotly.subplots import make_subplots
7476
import numpy as np
7577

@@ -90,23 +92,23 @@ for i in [1,2]:
9092
),
9193
row=i, col=j)
9294

93-
fig.update(layout_width=700, layout_margin=dict(r=10, l=10, b=10, t=10))
95+
fig.update_layout(width=700, margin=dict(r=10, l=10, b=10, t=10))
9496
# fix the ratio in the top left subplot to be a cube
95-
fig.update(layout_scene_aspectmode='cube')
97+
fig.update_layout(scene_aspectmode='cube')
9698
# manually force the z-axis to appear twice as big as the other two
97-
fig.update(layout_scene2_aspectmode='manual',
98-
layout_scene2_aspectratio=dict(x=1, y=1, z=2))
99+
fig.update_layout(scene2_aspectmode='manual',
100+
scene2_aspectratio=dict(x=1, y=1, z=2))
99101
# draw axes in proportion to the proportion of their ranges
100-
fig.update(layout_scene3_aspectmode='data')
102+
fig.update_layout(scene3_aspectmode='data')
101103
# automatically produce something that is well proportioned using 'data' as the default
102-
fig.update(layout_scene4_aspectmode='auto')
104+
fig.update_layout(scene4_aspectmode='auto')
103105
fig.show()
104106
```
105107

106108
### Set Axes Title
107109

108110
```python
109-
import plotly.graph_objs as go
111+
import plotly.graph_objects as go
110112
import numpy as np
111113

112114
# Define random surface
@@ -125,20 +127,20 @@ fig.add_trace(go.Mesh3d(x=(70*np.random.randn(N)),
125127
color='pink'
126128
))
127129

128-
fig.update(layout=go.Layout(scene = dict(
129-
xaxis_title='X AXIS TITLE',
130-
yaxis_title='Y AXIS TITLE',
131-
zaxis_title='Z AXIS TITLE'),
132-
width=700,
133-
margin=dict(r=20, b=10, l=10, t=10)))
130+
fig.update_layout(scene = dict(
131+
xaxis_title='X AXIS TITLE',
132+
yaxis_title='Y AXIS TITLE',
133+
zaxis_title='Z AXIS TITLE'),
134+
width=700,
135+
margin=dict(r=20, b=10, l=10, t=10))
134136

135137
fig.show()
136138
```
137139

138140
### Ticks Formatting
139141

140142
```python
141-
import plotly.graph_objs as go
143+
import plotly.graph_objects as go
142144
import numpy as np
143145

144146
# Define random surface
@@ -151,8 +153,7 @@ fig = go.Figure(data=[go.Mesh3d(x=(60*np.random.randn(N)),
151153
)])
152154

153155
# Different types of customized ticks
154-
fig.update(layout=go.Layout(
155-
scene = dict(
156+
fig.update_layout(scene = dict(
156157
xaxis = dict(
157158
ticktext= ['TICKS','MESH','PLOTLY','PYTHON'],
158159
tickvals= [0,50,75,-50]),
@@ -167,15 +168,15 @@ fig.update(layout=go.Layout(
167168
tick0=0, tickwidth=4),),
168169
width=700,
169170
margin=dict(r=10, l=10, b=10, t=10)
170-
))
171+
)
171172

172173
fig.show()
173174
```
174175

175176
### Background and Grid Color
176177

177178
```python
178-
import plotly.graph_objs as go
179+
import plotly.graph_objects as go
179180
import numpy as np
180181

181182
N = 50
@@ -186,8 +187,7 @@ fig = go.Figure(data=[go.Mesh3d(x=(30*np.random.randn(N)),
186187

187188

188189
# xaxis.backgroundcolor is used to set background color
189-
fig.update(layout = go.Layout(
190-
scene = dict(
190+
fig.update_layout(scene = dict(
191191
xaxis = dict(
192192
backgroundcolor="rgb(200, 200, 230)",
193193
gridcolor="white",
@@ -207,10 +207,6 @@ fig.update(layout = go.Layout(
207207
margin=dict(
208208
r=10, l=10,
209209
b=10, t=10)
210-
))
210+
)
211211
fig.show()
212212
```
213-
214-
```python
215-
216-
```

notebooks/3d-mesh.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ jupyter:
4242
`go.Mesh3d` draws a 3D set of triangles with vertices given by `x`, `y` and `z`. If only coordinates are given, an algorithm such as [Delaunay triangulation](https://en.wikipedia.org/wiki/Delaunay_triangulation) is used to draw the triangles. Otherwise the triangles can be given using the `i`, `j` and `k` parameters (see examples below).
4343

4444
```python
45-
import plotly.graph_objs as go
45+
import plotly.graph_objects as go
4646
import numpy as np
4747

4848
# Download data set from plotly repo
49-
ds = np.DataSource()
50-
pts = np.loadtxt(ds.open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
49+
pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
5150
x, y, z = pts.T
5251

5352
fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z, color='lightpink', opacity=0.50)])
@@ -60,11 +59,10 @@ fig.show()
6059
The `alphahull` parameter sets the shape of the mesh. If the value is -1 (default value) then [Delaunay triangulation](https://en.wikipedia.org/wiki/Delaunay_triangulation) is used. If >0 then the [alpha-shape algorithm](https://en.wikipedia.org/wiki/Alpha_shape) is used. If 0, the [convex hull](https://en.wikipedia.org/wiki/Convex_hull) is represented (resulting in a convex body).
6160

6261
```python
63-
import plotly.graph_objs as go
62+
import plotly.graph_objects as go
6463
import numpy as np
6564

66-
ds = np.DataSource()
67-
pts = np.loadtxt(ds.open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
65+
pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt'))
6866
x, y, z = pts.T
6967

7068
fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z,
@@ -79,7 +77,7 @@ fig.show()
7977
In this example we use the `ì`, `j` and `k` parameters to specify manually the geometry of the triangles of the mesh.
8078

8179
```python
82-
import plotly.graph_objs as go
80+
import plotly.graph_objects as go
8381

8482
fig = go.Figure(data=[
8583
go.Mesh3d(
@@ -108,7 +106,7 @@ fig.show()
108106
### Mesh Cube
109107

110108
```python
111-
import plotly.graph_objs as go
109+
import plotly.graph_objects as go
112110
fig = go.Figure(data=[
113111
go.Mesh3d(
114112
# 8 vertices of a cube

notebooks/3d-scatter-plots.md

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,45 +37,72 @@ jupyter:
3737
v4upgrade: true
3838
---
3939

40+
## 3D scatter plot with plotly express
41+
42+
Like the [2D scatter plot](https://plot.ly/python/line-and-scatter/) `px.scatter`, the 3D function `px.scatter_3d` plots individual data in three-dimensional space. Note that plotly express functions take as argument a tidy [pandas DataFrame](https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html) such as the ones defined in ``px.data``.
43+
44+
```python
45+
import plotly.express as px
46+
iris = px.data.iris()
47+
fig = px.scatter_3d(iris, x='sepal_length', y='sepal_width', z='petal_width',
48+
color='species')
49+
fig.show()
50+
```
51+
52+
A 4th dimension of the data can be represented thanks to the color of the markers. Also, values from the `species` column are used below to assign symbols to markers.
53+
54+
```python
55+
import plotly.express as px
56+
iris = px.data.iris()
57+
fig = px.scatter_3d(iris, x='sepal_length', y='sepal_width', z='petal_width',
58+
color='petal_length', symbol='species')
59+
fig.show()
60+
```
61+
62+
#### Style 3d scatter plot
63+
64+
It is possible to customize the style of the figure through the parameters of `px.scatter_3d` for some options, or by updating the traces or the layout of the figure through `fig.update`.
65+
66+
```python
67+
import plotly.express as px
68+
iris = px.data.iris()
69+
fig = px.scatter_3d(iris, x='sepal_length', y='sepal_width', z='petal_width',
70+
color='petal_length', size='petal_length', size_max=18,
71+
symbol='species', opacity=0.7)
72+
73+
# tight layout
74+
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
75+
```
76+
77+
### 3D Scatter Plot with go.Scatter3d
78+
4079
#### Basic 3D Scatter Plot
4180

42-
Like the [2D scatter plot](https://plot.ly/python/line-and-scatter/) `go.Scatter`, the 3D function `go.Scatter3d` plots individual data in three-dimensional space.
81+
When data are not available as tidy dataframes, it is also possible to use the more generic `go.Scatter3D` from `plotly.graph_objs`.
82+
Like the [2D scatter plot](https://plot.ly/python/line-and-scatter/) `go.Scatter`, `go.Scatter3d` plots individual data in three-dimensional space.
4383

4484
```python
45-
import plotly.graph_objs as go
85+
import plotly.graph_objects as go
4686
import numpy as np
4787

48-
fig = go.Figure()
49-
50-
x0, y0, z0 = np.random.multivariate_normal(np.array([0, 0, 0]), np.eye(3), 100).transpose()
51-
fig.add_trace(go.Scatter3d(
52-
x=x0, y=y0, z=z0,
53-
mode='markers'
54-
))
88+
# Helix equation
89+
t = np.linspace(0, 10, 50)
90+
x, y, z = np.cos(t), np.sin(t), t
5591

56-
x1, y1, z1 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 100).transpose()
57-
fig.add_trace(go.Scatter3d(
58-
x=x1, y=y1, z=z1,
59-
mode='markers',
60-
marker_color='gray',
61-
))
62-
63-
# tight layout
64-
fig.update(layout_margin=dict(l=0, r=0, b=0, t=0))
65-
# marker common configuration for the two Scatter3d
66-
fig.update_traces(marker_line_color='lightgray', marker_opacity=0.8, marker_size=12,
67-
marker_line_width=1)
92+
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z,
93+
mode='markers')])
6894
fig.show()
6995
```
7096

71-
#### 3D Scatter Plot with Colorscaling
97+
#### 3D Scatter Plot with Colorscaling and Marker Styling
7298

7399
```python
74-
import plotly.graph_objs as go
75-
100+
import plotly.graph_objects as go
76101
import numpy as np
77102

78-
x, y, z = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 400).transpose()
103+
# Helix equation
104+
t = np.linspace(0, 20, 100)
105+
x, y, z = np.cos(t), np.sin(t), t
79106

80107
fig = go.Figure(data=[go.Scatter3d(
81108
x=x,
@@ -91,7 +118,7 @@ fig = go.Figure(data=[go.Scatter3d(
91118
)])
92119

93120
# tight layout
94-
fig.update(layout_margin=dict(l=0, r=0, b=0, t=0))
121+
fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
95122
fig.show()
96123
```
97124

notebooks/3d-surface-plots.md

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jupyter:
4040
#### Topographical 3D Surface Plot
4141

4242
```python
43-
import plotly.graph_objs as go
43+
import plotly.graph_objects as go
4444

4545
import pandas as pd
4646

@@ -49,10 +49,9 @@ z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/a
4949

5050
fig = go.Figure(data=[go.Surface(z=z_data.values)])
5151

52-
fig.update(layout=go.Layout(title='Mt Bruno Elevation', autosize=False,
53-
width=500, height=500,
54-
margin=dict(l=65, r=50, b=65, t=90)
55-
))
52+
fig.update_layout(title='Mt Bruno Elevation', autosize=False,
53+
width=500, height=500,
54+
margin=dict(l=65, r=50, b=65, t=90))
5655

5756
fig.show()
5857
```
@@ -63,7 +62,7 @@ fig.show()
6362
Display and customize contour data for each axis using the `contours` attribute ([reference](plot.ly/python/reference/#surface-contours)).
6463

6564
```python
66-
import plotly.graph_objs as go
65+
import plotly.graph_objects as go
6766

6867
import pandas as pd
6968

@@ -80,19 +79,19 @@ fig = go.Figure(data=[
8079
project_z=True
8180
))
8281
])
83-
fig.update(layout=go.Layout(title='Mt Bruno Elevation', autosize=False,
84-
scene_camera_eye=dict(x=1.87, y=0.88, z=-0.64),
85-
width=500, height=500,
86-
margin=dict(l=65, r=50, b=65, t=90)
87-
))
82+
fig.update_layout(title='Mt Bruno Elevation', autosize=False,
83+
scene_camera_eye=dict(x=1.87, y=0.88, z=-0.64),
84+
width=500, height=500,
85+
margin=dict(l=65, r=50, b=65, t=90)
86+
)
8887

8988
fig.show()
9089
```
9190

9291
#### Multiple 3D Surface Plots
9392

9493
```python
95-
import plotly.graph_objs as go
94+
import plotly.graph_objects as go
9695
import numpy as np
9796

9897
z1 = np.array([

0 commit comments

Comments
 (0)