要使用Python繪制3D動態(tài)模型,可以使用一些庫如matplotlib和mayavi。下面是一個簡單的例子使用matplotlib和numpy庫來創(chuàng)建一個旋轉(zhuǎn)的3D立方體的動態(tài)模型:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# 創(chuàng)建立方體的頂點
r = [-1, 1]
X, Y = np.meshgrid(r, r)
Z = np.ones_like(X)
verts = [list(zip(X.flatten(), Y.flatten(), Z.flatten())),
list(zip(X.flatten(), Y.flatten(), -Z.flatten())),
list(zip(X.flatten(), -Z.flatten(), Y.flatten())),
list(zip(X.flatten(), Z.flatten(), Y.flatten())),
list(zip(-Z.flatten(), Y.flatten(), X.flatten())),
list(zip(Z.flatten(), Y.flatten(), X.flatten()))]
# 繪制立方體
poly3d = [[verts[0], verts[1], verts[4], verts[5]],
[verts[2], verts[3], verts[4], verts[5]],
[verts[0], verts[1], verts[2], verts[3]]]
# 繪制每一幀
def animate(i):
ax.clear()
ax.view_init(elev=30, azim=i)
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='cyan', linewidths=1, edgecolors='r', alpha=.25))
return fig,
# 創(chuàng)建動畫
ani = animation.FuncAnimation(fig, animate, frames=360, interval=50, blit=False)
plt.show()
這段代碼會創(chuàng)建一個旋轉(zhuǎn)的3D立方體的動態(tài)模型。您可以根據(jù)自己的需求調(diào)整立方體的大小,顏色以及旋轉(zhuǎn)的速度等參數(shù)。希望對您有幫助!