Python绘制球面以及均匀分布取点

2021-06-30  本文已影响0人  升不上三段的大鱼

利用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个点的坐标为
z_n = (2n-1)/N-1
x_n = \sqrt{1-z_n^2} \cdot cos(2 \pi n \phi)
y_n = \sqrt{1-z_n^2} \cdot sin(2 \pi n \phi)

其中\phi \approx 0.618,是黄金分割比。

生成的点阵,被称为斐波那契网格。详细的可以看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

上一篇下一篇

猜你喜欢

热点阅读