使用 python 实现三维 Voronoi 图
时间: 2023-12-05 17:04:54 浏览: 508
以下是一个使用 Python 和 Matplotlib 库实现三维 Voronoi 图的示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from scipy.spatial import Voronoi
# Generate random points
points = np.random.rand(10, 3)
# Compute Voronoi diagram
vor = Voronoi(points)
# Plot Voronoi diagram
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for i, simplex in enumerate(vor.ridge_vertices):
simplex = np.asarray(simplex)
if np.all(simplex >= 0):
poly = [vor.vertices[simplex[j]] for j in range(simplex.shape[0])]
ax.add_collection3d(Poly3DCollection([poly], alpha=0.25))
ax.scatter(points[:, 0], points[:, 1], points[:, 2], c='k', alpha=1)
plt.show()
```
此代码生成 10 个随机点并计算其 Voronoi 图。然后,使用 `Poly3DCollection` 绘制由 Voronoi 图中的边界形成的多边形。最后,使用 `scatter` 函数将点绘制为黑色。结果是一个三维 Voronoi 图,其中随机点位于黑色点中。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)