voronoi plot 3d python
最编程
2024-06-27 07:50:43
...
在 Python 中制作 3D Voronoi 图可以使用 scipy
和 matplotlib
库。以下是制作 3D Voronoi 图的基本步骤:
- 导入必要的库
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_3d
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
- 生成三维随机点集
points = np.random.rand(10, 3)
- 计算 Voronoi 图
vor = Voronoi(points)
- 绘制 Voronoi 图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for simplex in vor.ridge_vertices:
if np.all(simplex >= 0):
ax.plot(points[simplex, 0], points[simplex, 1], points[simplex, 2], 'k-')
for i, region in enumerate(vor.regions):
if not region:
continue
vertices = vor.vertices[region]
ax.add_collection3d(Poly3DCollection([vertices], alpha=.25))
ax.scatter(points[:,0], points[:,1], points[:,2], c='b')
plt.show()
这将生成一个包含 10 个随机点的 3D Voronoi 图。您可以根据需要更改点数或其他参数,以适应您的特定应用场景。