Skip to content

Commit 1f2259c

Browse files
committed
3d notebooks
1 parent 4c5740b commit 1f2259c

File tree

4 files changed

+148
-340
lines changed

4 files changed

+148
-340
lines changed

notebooks/3d-axes.md

Lines changed: 43 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ jupyter:
77
format_version: '1.1'
88
jupytext_version: 1.1.1
99
kernelspec:
10-
display_name: Python 2
10+
display_name: Python 3
1111
language: python
12-
name: python2
12+
name: python3
1313
plotly:
1414
description: How to format axes of 3d plots in Python with Plotly.
1515
display_as: layout_opt
@@ -25,16 +25,13 @@ jupyter:
2525
title: Format 3d Axes | plotly
2626
---
2727

28-
#### New to Plotly?
29-
Plotly's Python library is free and open source! [Get started](https://plot.ly/python/getting-started/) by downloading the client and [reading the primer](https://plot.ly/python/getting-started/).
30-
<br>You can set up Plotly to work in [online](https://plot.ly/python/getting-started/#initialization-for-online-plotting) or [offline](https://plot.ly/python/getting-started/#initialization-for-offline-plotting) mode, or in [jupyter notebooks](https://plot.ly/python/getting-started/#start-plotting-online).
31-
<br>We also have a quick-reference [cheatsheet](https://images.plot.ly/plotly-documentation/images/python_cheat_sheet.pdf) (new!) to help you get started!
28+
### Range of axes
3229

30+
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.
3331

34-
### Range of axes
32+
For creating 3D charts, see [this page](https://plot.ly/python/3d-charts/).
3533

3634
```python
37-
import plotly.plotly as py
3835
import plotly.graph_objs as go
3936
import numpy as np
4037

@@ -49,38 +46,33 @@ trace1 = go.Mesh3d(x=(70*np.random.randn(N)),
4946
layout = go.Layout(
5047
scene = dict(
5148
xaxis = dict(
52-
nticks=4, range = [-100,100],),
49+
nticks=4, range=[-100,100],),
5350
yaxis = dict(
54-
nticks=4, range = [-50,100],),
51+
nticks=4, range=[-50,100],),
5552
zaxis = dict(
56-
nticks=4, range = [-100,100],),),
53+
nticks=4, range=[-100,100],),),
5754
width=700,
5855
margin=dict(
5956
r=20, l=10,
6057
b=10, t=10)
6158
)
6259
fig = go.Figure(data=[trace1], layout=layout)
63-
py.iplot(fig, filename='3d-axis-range')
60+
fig.show()
6461
```
6562

6663
### Fixed Ratio Axes
6764

6865
```python
69-
import plotly.plotly as py
7066
import plotly.graph_objs as go
71-
import plotly.tools as tls
67+
from plotly.subplots import make_subplots
7268
import numpy as np
7369

7470
N = 50
7571

76-
fig = tls.make_subplots(
77-
rows=2, cols=2,
78-
specs=[
79-
[{'is_3d': True}, {'is_3d': True}],
80-
[{'is_3d': True}, {'is_3d': True}]
81-
],
82-
print_grid=False
83-
)
72+
fig = make_subplots(rows=2, cols=2,
73+
specs=[[{'is_3d': True}, {'is_3d': True}],
74+
[{'is_3d': True}, {'is_3d': True}]],
75+
print_grid=False)
8476
for i in [1,2]:
8577
for j in [1,2]:
8678
fig.append_trace(
@@ -92,43 +84,26 @@ for i in [1,2]:
9284
),
9385
row=i, col=j)
9486

95-
fig['layout'].update(go.Layout(
96-
width=700,
97-
margin=dict(
98-
r=10, l=10,
99-
b=10, t=10)
100-
))
101-
87+
fig.update(layout_width=700, layout_margin=dict(r=10, l=10, b=10, t=10))
10288
# fix the ratio in the top left subplot to be a cube
103-
fig['layout'][].update(go.Layout(
104-
go.layout.Scene(aspectmode='cube')),
105-
106-
89+
fig['layout']['scene'].update(aspectmode='cube')
10790
# manually force the z-axis to appear twice as big as the other two
108-
fig['layout']['scene2'].update(go.layout.Scene(
109-
aspectmode='manual',
110-
aspectratio=go.layout.scene.Aspectratio(
111-
x=1, y=1, z=2
112-
)
113-
))
114-
91+
fig['layout']['scene2'].update(aspectmode='manual',
92+
aspectratio=dict(x=1, y=1, z=2))
11593
# draw axes in proportion to the proportion of their ranges
116-
fig['layout']['scene3'].update(go.layout.Scene(aspectmode='data'))
117-
94+
fig['layout']['scene3'].update(aspectmode='data')
11895
# automatically produce something that is well proportioned using 'data' as the default
119-
fig['layout']['scene4'].update(go.layout.Scene(aspectmode='auto'))
120-
121-
py.iplot(fig, filename='3d-axis-fixed-ratio-axes')
122-
96+
fig['layout']['scene4'].update(aspectmode='auto')
97+
fig.show()
12398
```
12499

125100
### Set Axes Title
126101

127102
```python
128-
import plotly.plotly as py
129103
import plotly.graph_objs as go
130104
import numpy as np
131105

106+
# Define random surface
132107
N = 50
133108
trace1 = go.Mesh3d(x=(60*np.random.randn(N)),
134109
y=(25*np.random.randn(N)),
@@ -142,30 +117,25 @@ trace2 = go.Mesh3d(x=(70*np.random.randn(N)),
142117
opacity=0.5,
143118
color='pink'
144119
)
145-
layout = go.Layout(
146-
scene = dict(
147-
xaxis = dict(
148-
title='X AXIS TITLE'),
149-
yaxis = dict(
150-
title='Y AXIS TITLE'),
151-
zaxis = dict(
152-
title='Z AXIS TITLE'),),
153-
width=700,
154-
margin=dict(
155-
r=20, b=10,
156-
l=10, t=10)
157-
)
120+
121+
layout = go.Layout(scene = dict(
122+
xaxis_title='X AXIS TITLE',
123+
yaxis_title='Y AXIS TITLE',
124+
zaxis_title='Z AXIS TITLE'),
125+
width=700,
126+
margin=dict(r=20, b=10, l=10, t=10))
127+
158128
fig = go.Figure(data=[trace1,trace2], layout=layout)
159-
py.iplot(fig, filename='3d-axis-titles')
129+
fig.show()
160130
```
161131

162132
### Ticks Formatting
163133

164134
```python
165-
import plotly.plotly as py
166135
import plotly.graph_objs as go
167136
import numpy as np
168137

138+
# Define random surface
169139
N = 50
170140
trace1 = go.Mesh3d(x=(60*np.random.randn(N)),
171141
y=(25*np.random.randn(N)),
@@ -174,6 +144,7 @@ trace1 = go.Mesh3d(x=(60*np.random.randn(N)),
174144
color='rgba(100,22,200,0.5)'
175145
)
176146

147+
# Different types of customized ticks
177148
layout = go.Layout(
178149
scene = dict(
179150
xaxis = dict(
@@ -189,18 +160,15 @@ layout = go.Layout(
189160
nticks=4, ticks='outside',
190161
tick0=0, tickwidth=4),),
191162
width=700,
192-
margin=dict(
193-
r=10, l=10,
194-
b=10, t=10)
163+
margin=dict(r=10, l=10, b=10, t=10)
195164
)
196165
fig = go.Figure(data=[trace1], layout=layout)
197-
py.iplot(fig, filename='3d-axis-tick-formatting')
166+
fig.show()
198167
```
199168

200169
### Background and Grid Color
201170

202171
```python
203-
import plotly.plotly as py
204172
import plotly.graph_objs as go
205173
import numpy as np
206174

@@ -211,51 +179,29 @@ trace1 = go.Mesh3d(x=(30*np.random.randn(N)),
211179
opacity=0.5,)
212180

213181

182+
# xaxis.backgroundcolor is used to set background color
214183
layout = go.Layout(
215184
scene = dict(
216185
xaxis = dict(
217186
backgroundcolor="rgb(200, 200, 230)",
218-
gridcolor="rgb(255, 255, 255)",
187+
gridcolor="white",
219188
showbackground=True,
220-
zerolinecolor="rgb(255, 255, 255)",),
189+
zerolinecolor="white",),
221190
yaxis = dict(
222191
backgroundcolor="rgb(230, 200,230)",
223-
gridcolor="rgb(255, 255, 255)",
192+
gridcolor="white",
224193
showbackground=True,
225-
zerolinecolor="rgb(255, 255, 255)"),
194+
zerolinecolor="white"),
226195
zaxis = dict(
227196
backgroundcolor="rgb(230, 230,200)",
228-
gridcolor="rgb(255, 255, 255)",
197+
gridcolor="white",
229198
showbackground=True,
230-
zerolinecolor="rgb(255, 255, 255)",),),
199+
zerolinecolor="white",),),
231200
width=700,
232201
margin=dict(
233202
r=10, l=10,
234203
b=10, t=10)
235204
)
236205
fig = go.Figure(data=[trace1], layout=layout)
237-
py.iplot(fig, filename='3d-axis-background-and-grid-color')
206+
fig.show()
238207
```
239-
240-
```python
241-
from IPython.display import display, HTML
242-
243-
display(HTML('<link href="//fonts.googleapis.com/css?family=Open+Sans:600,400,300,200|Inconsolata|Ubuntu+Mono:400,700" rel="stylesheet" type="text/css" />'))
244-
display(HTML('<link rel="stylesheet" type="text/css" href="http://help.plot.ly/documentation/all_static/css/ipython-notebook-custom.css">'))
245-
246-
! pip install git+https://github.com/plotly/publisher.git --upgrade
247-
import publisher
248-
publisher.publish(
249-
'3d-axes.ipynb', 'python/3d-axes/', 'Axes Formatting in 3d Plots | plotly',
250-
'How to format axes of 3d plots in Python with Plotly.',
251-
title = 'Format 3d Axes | plotly',
252-
name = '3D Axes',
253-
has_thumbnail='false', thumbnail='thumbnail/your-tutorial-chart.jpg',
254-
language='python', page_type='example_index',
255-
display_as='layout_opt', order=1,
256-
ipynb= '~notebook_demo/96')
257-
```
258-
259-
```python
260-
261-
```

0 commit comments

Comments
 (0)