python matplatlib 画边长为1的正四面体和正四棱锥

本文介绍了如何使用Python的Matplotlib库来绘制边长为1的三维几何图形,包括正四面体和正四棱锥。通过实例代码详细解释了绘制过程,帮助读者掌握在Python中创建三维图形的方法。
摘要由CSDN通过智能技术生成
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 &
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值