import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_xlabel('X')
ax.set_xlim3d(0, 2)
ax.set_ylabel('Y')
ax.set_ylim3d(0, 1)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
# 画正四棱锥
# 顶点坐标
verts = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0), (0.5, 0.5, 0.707)]
# 面
faces = [[0, 1, 4], [1, 2, 4], [2, 3, 4], [0, 3, 4], [0, 1, 2, 3]]
# 每个面对应的点坐标
poly3d = [[verts[vert_id] for vert_id in face] for face in faces]# 画顶点
x, y, z = zip(*verts)
ax.scatter(x, y, z)
collection = Poly3DCollection(poly3d, edgecolors= 'r', facecolor= [0.5, 0.5, 1], linewidths=1, alpha=0.3)
ax.add_collection3d(collection)
#ax.add_collection3d(Line3DCollection(poly3d, colors='b', linewidths=0.5))
# 画正四面体
verts1 = [(1, 1, 0), (1, 0, 0), (1.5, 0.5, 0.707), (0.5, 0.5, 0.707)]
faces1 &
python matplatlib 画边长为1的正四面体和正四棱锥
最新推荐文章于 2024-08-17 03:23:00 发布
本文介绍了如何使用Python的Matplotlib库来绘制边长为1的三维几何图形,包括正四面体和正四棱锥。通过实例代码详细解释了绘制过程,帮助读者掌握在Python中创建三维图形的方法。
摘要由CSDN通过智能技术生成