欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

voronoi plot 3d python

最编程 2024-06-27 07:50:43
...

在 Python 中制作 3D Voronoi 图可以使用 scipymatplotlib 库。以下是制作 3D Voronoi 图的基本步骤:

  1. 导入必要的库
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_3d
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
  1. 生成三维随机点集
points = np.random.rand(10, 3)
  1. 计算 Voronoi 图
vor = Voronoi(points)
  1. 绘制 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 图。您可以根据需要更改点数或其他参数,以适应您的特定应用场景。