利用matplotlib.pyplot库绘制三维的球面
import numpy as np
import matplotlib.pyplot as plt
fig1 = plt.figure("sphere points 1")
ax1 = fig1.add_subplot(111,projection='3d')
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 50)
x = np.outer(np.cos(u), np.sin(v))
y = np.outer(np.sin(u), np.sin(v))
z = np.outer(np.ones(np.size(u)), np.cos(v))
ax.plot_surface(x, y, z, rstride=4, cstride=4, color='b', linewidth=0, alpha=0.5)
#生成散点图
ax1.scatter(x,y,z)
sphere_points_1.png
对应的三维散点图
从上面图中可以看到在两极的点更加密集,如果想要更加均匀分布的点,可以根据以下方法(Evenly distributing n points on a sphere):
设球面半径为1,球面上共N个点,第n个点的坐标为
其中,是黄金分割比。
生成的点阵,被称为斐波那契网格。详细的可以看10560 怎样在球面上「均匀」排列许多点?(上)
N = 3000
points = [[0,0,0] for _ in range(N)]
phi = 0.618
for n in range(N):
z = (2*n-1)/N-1
x = np.sqrt(1 - z * z) * np.cos(2 * np.pi * n * phi)
y = np.sqrt(1 - z * z) * np.sin(2 * np.pi * n * phi)
points[n][0] = x
points[n][1] = y
points[n][2] = z
points = np.array(points)
fig2 = plt.figure("sphere points uniform")
ax2 = fig2.add_subplot(111,projection='3d')
ax2.scatter(points[:,0], points[:,1], points[:,2], color='r')
ax2.scatter(points[:,0], points[:,1], -points[:,2], color='r')
plt.show()
均匀分布的点
参考:
https://zhuanlan.zhihu.com/p/103715075
https://zhuanlan.zhihu.com/p/26052376
网友评论