要繪制3D圖形,可以使用Matplotlib中的mplot3d模塊。以下是一個(gè)簡(jiǎn)單的示例,展示如何繪制一個(gè)立方體:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 定義立方體的頂點(diǎn)
vertices = [
[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]
]
# 定義立方體的面
faces = [
[vertices[0], vertices[1], vertices[2], vertices[3]],
[vertices[4], vertices[5], vertices[6], vertices[7]],
[vertices[0], vertices[1], vertices[5], vertices[4]],
[vertices[2], vertices[3], vertices[7], vertices[6]],
[vertices[1], vertices[2], vertices[6], vertices[5]],
[vertices[4], vertices[7], vertices[3], vertices[0]]
]
# 繪制立方體的面
for face in faces:
x = [vertex[0] for vertex in face]
y = [vertex[1] for vertex in face]
z = [vertex[2] for vertex in face]
ax.plot(x, y, z)
plt.show()
運(yùn)行以上代碼即可繪制一個(gè)簡(jiǎn)單的立方體。您也可以根據(jù)需要調(diào)整頂點(diǎn)和面的位置,以繪制不同的3D圖形。