1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import matplotlib as mpl import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import numpy as np from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
def plot_body_3d(title, verts, faces): ''' 绘制3D实体图 ''' fig = plt.figure(figsize=(16, 10), dpi= 80) ax = Axes3D(fig)
poly3d = [[verts[vert_id] for vert_id in face] for face in faces] x, y, z = zip(*verts) ax.scatter(x, y, z)
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.3)) ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.5, linestyles=':'))
ax.set_xlabel('x', fontsize=16) ax.set_ylabel('y', fontsize=16) ax.set_zlabel('z', fontsize=16) ax.view_init(elev=10, azim=235)
plt.title(title, fontsize=22) plt.grid(False) ax.set_aspect('equal') plt.show()
verts = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0), (0, 0, 1), (0, 1, 1), (1, 1, 1), (1, 0, 1)] faces = [[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [0, 3, 7, 4]]
plot_body_3d('测试图', verts, faces)
|