如何使用 matplotlib 在 3D 中绘制六边形



我通过搜索尝试了一些东西,但我缺少对顶点的理解,或者至少目前大脑褪色的东西,有人可以帮助我,我需要正六边形

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
fig = plt.figure(figsize=(15,9))
ax = fig.add_subplot(111, projection='3d')
x = [0, 2, 1, 1,1,1]
y = [0, 0, 1, 0, 1,1]
z = [0, 0, 0, 1,1,1]
vertices = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3],[0,1,2],[0,1,2]]
tupleList = list(zip(x, y, z))
poly3d = [[tupleList[vertices[ix][iy]] for iy in range(len(vertices[0]))] for ix in range(len(vertices))]
ax.scatter(x,y,z)
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.5))
ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.2, linestyles=':'))
plt.show()

Matplotlib 不适用于真正的 3D。matplotlib中的3D内容主要只是为了更好的2D数据外观。如果您需要真正的3D可视化,我推荐Mayavi或VTK。

如果你的六边形不能表示为 2 个变量的数学函数(例如 z = f(x,y)),那么 matplotlib 是错误的工具。

最新更新