You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用Matplotlib绘制3D图时如何连接顶点?

你当前代码仅调用一次ax.plot(),方法会按照传入坐标的顺序依次连线,无法自动生成目标效果需要的所有棱边,你可以手动拆分点集后逐类绘制所需连线,修改后的代码如下:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig3 = plt.figure()
ax = fig3.add_subplot(1,1,1,projection='3d')

# 拆分点集:底面4点、顶面4点、顶部尖点
bottom_points = [(2,0,0), (3,0,0), (3,1,0), (2,1,0)]
top_plane_points = [(2,0,1), (3,0,1), (3,1,1), (2,1,1)]
peak_point = (2.5, 0.5, 2)

# 绘制底面正方形
bx, by, bz = zip(*(bottom_points + [bottom_points[0]]))
ax.plot(bx, by, bz, color='#1f77b4')

# 绘制顶面正方形
tx, ty, tz = zip(*(top_plane_points + [top_plane_points[0]]))
ax.plot(tx, ty, tz, color='#1f77b4')

# 绘制底面到顶面的竖边
for b, t in zip(bottom_points, top_plane_points):
    ax.plot([b[0], t[0]], [b[1], t[1]], [b[2], t[2]], color='#1f77b4')

# 绘制顶面到尖点的连线
for p in top_plane_points:
    ax.plot([p[0], peak_point[0]], [p[1], peak_point[1]], [p[2], peak_point[2]], color='#1f77b4')

# 调整视角方便观察,可根据需求修改
ax.view_init(elev=30, azim=45)

plt.show()

如果需要调整线条颜色、粗细等样式,直接给ax.plot()传入c、linewidth等参数即可。

内容的提问来源于stack exchange,提问作者dulzdude

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 21:18:00