【发布时间】:2020-12-13 10:13:24
【问题描述】:
我正在尝试使用 FuncAnimation 在 matplotlib 中旋转 3D 立方体。然而,对于某种 70 年代的织物艺术来说,它不仅仅是显示一个立方体的单个渲染,它只是在自身上绘制动画。
在许多其他错误开始之后,这是我得到的最接近的结果,但显然我没有正确使用 FuncAnimation。对于我哪里出错的任何提示和解释,我将不胜感激。 (这是在 Jupyter 笔记本中运行的)
%matplotlib notebook
import numpy as np
from numpy import sin, cos, pi
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
rot_num = 1 # number of rotations
smoothness = 90 # how many steps per rotation
# Define corners of a cube
cube = np.array([[0,0,1],[1,0,1],[1,1,1],[0,1,1],[0,0,0],[1,0,0],[1,1,0],[0,1,0]])
angles = np.linspace(0, rot_num*2*pi, smoothness*rot_num)
points = np.zeros(shape=(len(cube), 3, len(angles)), dtype=np.float16)
# Calculate all the points needed for rotation
for i in range(len(points)):
newX = cube[i,0] * cos(angles) - cube[i,2] * sin(angles)
newY = cube[i,1]
newZ = cube[i,2] * cos(angles) + cube[i,0] * sin(angles)
points[i,0] = newX
points[i,1] = newY
points[i,2] = newZ
# Define the vertices/lines of the cube using corners, with color
cube_v = [[points[0], points[1], "green"],
[points[1], points[2], "green"],
[points[2], points[3], "green"],
[points[3], points[0], "green"],
[points[0], points[4], "blue"],
[points[1], points[5], "blue"],
[points[2], points[6], "blue"],
[points[3], points[7], "blue"],
[points[4], points[5], "red"],
[points[5], points[6], "red"],
[points[6], points[7], "red"],
[points[7], points[4], "red"]]
fig = plt.figure()
plt.rcParams["figure.figsize"] = 9,9
ax = fig.add_subplot(111, projection="3d", autoscale_on=True)
ax.grid()
ax.set_title('3D Animation')
ax.set_xlim3d([-2.0, 2.0])
ax.set_xlabel('X')
ax.set_ylim3d([-2.0, 2.0])
ax.set_ylabel('Y')
ax.set_zlim3d([-2.0, 2.0])
ax.set_zlabel('Z')
def update(i):
for vertex in cube_v:
line = ax.plot([vertex[0][0][i], vertex[1][0][i]],
[vertex[0][1][i], vertex[1][1][i]],
[vertex[0][2][i], vertex[1][2][i]],
vertex[2])
ani = animation.FuncAnimation(fig, update, frames=len(angles), interval=20, blit=False, repeat=False)
plt.show()
【问题讨论】:
-
在创建新的
plot()之前,您必须先clear()plot/figure/ax。或者您必须只创建/绘制line一次,以后只需替换现有line中的data
标签: python matplotlib animation 3d