plotly系列(4)-- 绘制三维图表

使用plotly绘制三维图表的方法和绘制二维图表类似,也不需要引入额外模块。

1、三维空间中的点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
import plotly as py
import plotly.graph_objs as go

x1, y1, z1 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 200).transpose()
trace1 = go.Scatter3d(
x=x1,
y=y1,
z=z1,
mode='markers',
marker=dict(
color='rgb(217, 217, 217)',
size=12,
symbol='circle',
opacity=0.8
)
)

x2, y2, z2 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 200).transpose()
trace2 = go.Scatter3d(
x=x2,
y=y2,
z=z2,
mode='markers',
marker=dict(
color='rgb(127, 127, 127)',
size=12,
symbol='circle',
opacity=0.8
)
)

data = [trace1, trace2]

layout = go.Layout(
title='三维散点',
autosize=True,
margin=dict(
l=65,
r=50,
b=65,
t=90
)
)

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

2、三维空间中的线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import numpy as np
import plotly as py
import plotly.graph_objs as go

def f(x, y):
z = np.power(x, 2) + np.power(y, 2)
return z

x = y = np.linspace(start=0, stop=10, num=1000)
z = f(x, y)

trace = go.Scatter3d(
x=x,
y=y,
z=z,
mode='lines'
)

data = [trace]

layout = go.Layout(
title='三维线',
autosize=True,
margin=dict(
l=65,
r=50,
b=65,
t=90
)
)

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)

fig.show()

3、三维空间中的面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
import plotly as py
import plotly.graph_objs as go

def f(x, y):
z = np.power(x, 2) + np.power(y, 2)
return z

a = np.linspace(start=0, stop=3, num=4, dtype=np.int32)
b = np.linspace(start=0, stop=3, num=4, dtype=np.int32)

x, y = np.meshgrid(a, b)
z = f(x, y)


trace = go.Surface(x=x, y=y, z=z)
data = [trace]

layout = go.Layout(
title='三维曲面',
autosize=True,
margin=dict(
l=65,
r=50,
b=65,
t=90
)
)

fig = go.Figure(data=data, layout=layout)

py.offline.plot(fig, filename='test.html', auto_open=True)
0%