Python 如何在动画结束后继续计算而不关闭图形窗口
我有6个IMU传感器。我可以从Python中读取它们。我想用传感器读数做一些人体姿势的动画。动画需要处于Python 如何在动画结束后继续计算而不关闭图形窗口,python,animation,Python,Animation,我有6个IMU传感器。我可以从Python中读取它们。我想用传感器读数做一些人体姿势的动画。动画需要处于while(True)循环中。所以我的问题是: 我实现了如下动画功能: # import necessary packages import numpy as np from numpy import pi, sin, cos, sqrt import matplotlib.pyplot as plt import matplotlib.animation as animation # in
while(True)
循环中。所以我的问题是:
我实现了如下动画功能:
# import necessary packages
import numpy as np
from numpy import pi, sin, cos, sqrt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# input parameters
r = 1.0 # crank radius
l = 4.0 # connecting rod length
rot_num = 1 # number of crank rotations
increment = 0.1 # angle increment
# create the angle array, where the last angle is the number of rotations*2*pi
angles = np.arange(0, rot_num * 2 * pi + increment, increment)
X1 = np.zeros(len(angles)) # array of crank x-positions: Point 1
Y1 = np.zeros(len(angles)) # array of crank y-positions: Point 1
X2 = np.zeros(len(angles)) # array of rod x-positions: Point 2
Y2 = np.zeros(len(angles)) # array of rod y-positions: Point 2
# find the crank and connecting rod positions for each angle
for index, theta in enumerate(angles, start=0):
x1 = r * cos(theta) # x-cooridnate of the crank: Point 1
y1 = r * sin(theta) # y-cooridnate of the crank: Point 1
x2 = 0 # x-coordinate of the rod: Point 2
# y-coordinate of the rod: Point 2
y2 = r * sin(theta) + sqrt(l ** 2 - (r * cos(theta)) ** 2)
X1[index] = x1 # crankshaft x-position
Y1[index] = y1 # crankshaft y-position
X2[index] = x2 # connecting rod x-position
Y2[index] = y2 # connecting rod y-position
# set up the figure and subplot
fig = plt.figure()
fig.canvas.set_window_title('Matplotlib Animation')
ax = fig.add_subplot(111, aspect='equal', autoscale_on=False, xlim=(-4, 4), ylim=(-2, 6))
ax.grid()
ax.set_title('Piston Motion Animation')
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
line, = ax.plot([], [], 'o-', lw=5, color='#de2d26')
plt.ion()
plt.show()
# initialization function
def init():
line.set_data([], [])
return line,
# animation function
def animate1(i):
x_points = [0, X1[i], X2[i]]
y_points = [0, Y1[i], Y2[i]]
line.set_data(x_points, y_points)
return line,
def animate2(i):
x_points = [0, X1[i], X2[i]]
y_points = [0, Y1[i], Y2[i]]
line.set_data(x_points, y_points)
return line,
# call the animation
ani1 = animation.FuncAnimation(fig, animate1, init_func=init, frames=len(X1), interval=40, blit=True, repeat=False)
plt.draw()
plt.pause(0.001)
# to save animation, uncomment the line below:
# ani.save('offset_piston_motion_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
# show the animation
print('Continue')
plt.show()
它会导致程序冻结。因为我需要调用动画,然后返回到循环的开始并重复计算,当然还要再次设置人体姿势的动画,所以当我到达动画命令时,我需要以某种方式继续编程计算。在线找到的当前解决方案(写在代码中)将关闭动画打印窗口以继续,但我需要保持动画打印窗口打开,因为下一个动画应该从最后一个姿势开始。我不知道如何做到这一点。我放在这里的代码是一个示例动画
ani1 = animation.FuncAnimation(fig, animate1, init_func=init,
frames=len(X1), interval=40, blit=True, repeat=False)