7
7
format_version : ' 1.1'
8
8
jupytext_version : 1.1.1
9
9
kernelspec :
10
- display_name : Python 2
10
+ display_name : Python 3
11
11
language : python
12
- name : python2
12
+ name : python3
13
13
plotly :
14
14
description : How to format axes of 3d plots in Python with Plotly.
15
15
display_as : layout_opt
@@ -25,16 +25,13 @@ jupyter:
25
25
title : Format 3d Axes | plotly
26
26
---
27
27
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
32
29
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.
33
31
34
- ### Range of axes
32
+ For creating 3D charts, see [ this page ] ( https://plot.ly/python/3d-charts/ ) .
35
33
36
34
``` python
37
- import plotly.plotly as py
38
35
import plotly.graph_objs as go
39
36
import numpy as np
40
37
@@ -49,38 +46,33 @@ trace1 = go.Mesh3d(x=(70*np.random.randn(N)),
49
46
layout = go.Layout(
50
47
scene = dict (
51
48
xaxis = dict (
52
- nticks = 4 , range = [- 100 ,100 ],),
49
+ nticks = 4 , range = [- 100 ,100 ],),
53
50
yaxis = dict (
54
- nticks = 4 , range = [- 50 ,100 ],),
51
+ nticks = 4 , range = [- 50 ,100 ],),
55
52
zaxis = dict (
56
- nticks = 4 , range = [- 100 ,100 ],),),
53
+ nticks = 4 , range = [- 100 ,100 ],),),
57
54
width = 700 ,
58
55
margin = dict (
59
56
r = 20 , l = 10 ,
60
57
b = 10 , t = 10 )
61
58
)
62
59
fig = go.Figure(data = [trace1], layout = layout)
63
- py.iplot(fig, filename = ' 3d-axis-range ' )
60
+ fig.show( )
64
61
```
65
62
66
63
### Fixed Ratio Axes
67
64
68
65
``` python
69
- import plotly.plotly as py
70
66
import plotly.graph_objs as go
71
- import plotly.tools as tls
67
+ from plotly.subplots import make_subplots
72
68
import numpy as np
73
69
74
70
N = 50
75
71
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 )
84
76
for i in [1 ,2 ]:
85
77
for j in [1 ,2 ]:
86
78
fig.append_trace(
@@ -92,43 +84,26 @@ for i in [1,2]:
92
84
),
93
85
row = i, col = j)
94
86
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 ))
102
88
# 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' )
107
90
# 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 ))
115
93
# 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' )
118
95
# 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()
123
98
```
124
99
125
100
### Set Axes Title
126
101
127
102
``` python
128
- import plotly.plotly as py
129
103
import plotly.graph_objs as go
130
104
import numpy as np
131
105
106
+ # Define random surface
132
107
N = 50
133
108
trace1 = go.Mesh3d(x = (60 * np.random.randn(N)),
134
109
y = (25 * np.random.randn(N)),
@@ -142,30 +117,25 @@ trace2 = go.Mesh3d(x=(70*np.random.randn(N)),
142
117
opacity = 0.5 ,
143
118
color = ' pink'
144
119
)
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
+
158
128
fig = go.Figure(data = [trace1,trace2], layout = layout)
159
- py.iplot(fig, filename = ' 3d-axis-titles ' )
129
+ fig.show( )
160
130
```
161
131
162
132
### Ticks Formatting
163
133
164
134
``` python
165
- import plotly.plotly as py
166
135
import plotly.graph_objs as go
167
136
import numpy as np
168
137
138
+ # Define random surface
169
139
N = 50
170
140
trace1 = go.Mesh3d(x = (60 * np.random.randn(N)),
171
141
y = (25 * np.random.randn(N)),
@@ -174,6 +144,7 @@ trace1 = go.Mesh3d(x=(60*np.random.randn(N)),
174
144
color = ' rgba(100,22,200,0.5)'
175
145
)
176
146
147
+ # Different types of customized ticks
177
148
layout = go.Layout(
178
149
scene = dict (
179
150
xaxis = dict (
@@ -189,18 +160,15 @@ layout = go.Layout(
189
160
nticks = 4 , ticks = ' outside' ,
190
161
tick0 = 0 , tickwidth = 4 ),),
191
162
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 )
195
164
)
196
165
fig = go.Figure(data = [trace1], layout = layout)
197
- py.iplot(fig, filename = ' 3d-axis-tick-formatting ' )
166
+ fig.show( )
198
167
```
199
168
200
169
### Background and Grid Color
201
170
202
171
``` python
203
- import plotly.plotly as py
204
172
import plotly.graph_objs as go
205
173
import numpy as np
206
174
@@ -211,51 +179,29 @@ trace1 = go.Mesh3d(x=(30*np.random.randn(N)),
211
179
opacity = 0.5 ,)
212
180
213
181
182
+ # xaxis.backgroundcolor is used to set background color
214
183
layout = go.Layout(
215
184
scene = dict (
216
185
xaxis = dict (
217
186
backgroundcolor = " rgb(200, 200, 230)" ,
218
- gridcolor = " rgb(255, 255, 255) " ,
187
+ gridcolor = " white " ,
219
188
showbackground = True ,
220
- zerolinecolor = " rgb(255, 255, 255) " ,),
189
+ zerolinecolor = " white " ,),
221
190
yaxis = dict (
222
191
backgroundcolor = " rgb(230, 200,230)" ,
223
- gridcolor = " rgb(255, 255, 255) " ,
192
+ gridcolor = " white " ,
224
193
showbackground = True ,
225
- zerolinecolor = " rgb(255, 255, 255) " ),
194
+ zerolinecolor = " white " ),
226
195
zaxis = dict (
227
196
backgroundcolor = " rgb(230, 230,200)" ,
228
- gridcolor = " rgb(255, 255, 255) " ,
197
+ gridcolor = " white " ,
229
198
showbackground = True ,
230
- zerolinecolor = " rgb(255, 255, 255) " ,),),
199
+ zerolinecolor = " white " ,),),
231
200
width = 700 ,
232
201
margin = dict (
233
202
r = 10 , l = 10 ,
234
203
b = 10 , t = 10 )
235
204
)
236
205
fig = go.Figure(data = [trace1], layout = layout)
237
- py.iplot(fig, filename = ' 3d-axis-background-and-grid-color ' )
206
+ fig.show( )
238
207
```
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