FuncAnimation制作Matplotlib摆动画的问题

时间:2019-11-07 22:13:14

标签: python matplotlib animation pendulum

我要模拟的问题是一个简单的摆。在使用PyGame之前,我现在决定使用matplotlib的动画工具。它正在工作,但效果不理想。实时模拟它似乎是可行的。我已经间隔了帧的间隔和数量,但是fps太低了。如何在仍然实时播放的同时提高fps。我将不胜感激。无论如何,这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

g = 9.80665
L = 2
mu = 0.1
t = 100
theta_0 = np.pi/3
d_theta_0 = 0

def get_d2_theta(theta,d_theta):
    return -mu*d_theta-(g/L)*np.sin(theta)

def theta(t):
    theta = theta_0
    d_theta = d_theta_0
    delta_t = 1./60
    for time in np.arange(0,t,delta_t): 
        d2_theta = get_d2_theta(theta,d_theta)
        theta += d_theta*delta_t
        d_theta += d2_theta*delta_t
    return theta

x_data = [0,0]
y_data = [0,0]

fig, ax = plt.subplots()
ax.set_xlim(-2, 2)
ax.set_ylim(-2.5,1)
line, = ax.plot(0, 0)

def animation_frame(i):
    x = L*np.sin(theta(i))
    y = -L*np.cos(theta(i))

    x_data[1] = x
    y_data[1] = y

    line.set_xdata(x_data)
    line.set_ydata(y_data)
    return line, 

animation = FuncAnimation(fig, func=animation_frame, frames=np.arange(0, 60, (1./60)),interval = 10)
plt.show()

0 个答案:

没有答案