forked from bainingchao/PyDataPreprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3D.py
More file actions
69 lines (49 loc) · 2.08 KB
/
Copy path3D.py
File metadata and controls
69 lines (49 loc) · 2.08 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#coding: utf-8
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from mpl_toolkits.mplot3d import Axes3D
# 绘制3D梯度下降图
def d3_hookface():
fig = plt.figure() # 得到画面
ax = fig.gca(projection='3d') # 得到3d坐标的图
X = np.arange(-5, 5, 0.1)
Y = np.arange(-5, 5, 0.1)
X,Y = np.meshgrid(X, Y) # 将坐标向量变为坐标矩阵,列为x的长度,行为y的长度
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
# 曲面,x,y,z坐标,横向步长,纵向步长,颜色,线宽,是否渐变
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_zlim(-1.01, 1.01)
ax.set_xlabel("x-label", color='r')
ax.set_ylabel("y-label", color='g')
ax.set_zlabel("z-label", color='b')
ax.zaxis.set_major_locator(LinearLocator(10)) # 设置z轴标度
ax.zaxis.set_major_formatter(FormatStrFormatter('%0.02f')) # 设置z轴精度
# shrink颜色条伸缩比例0-1, aspect颜色条宽度(反比例,数值越大宽度越窄)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.savefig("d3_hookface.png")
plt.show()
def d3_scatter():
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 50
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('g', '+', -20, 15),('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
def randrange(n, vmin, vmax):
return (vmax - vmin) * np.random.rand(n) + vmin
if __name__ =='__main__':
# 绘制3D梯度下降图
# d3_hookface()
# 绘制3D散点图
d3_scatter()