python-3.x “FuncAnimation”对象没有属性“_resize_id”

mzaanser  于 2022-12-15  发布在  Python
关注(0)|答案(1)|浏览(1224)

我尝试使用欧拉方法绘制一个单摆,并使用给定的theta值和python中的公式,但我在FuncAnimation上收到一个属性错误,称“FuncAnimation”对象没有属性“_resize_id”。有人知道我在这里做错了什么吗?

# Liður 2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def ydot(t, y):
    g = 9.81
    l = 1
    z1 = y[1]
    z2 = -g/l*np.sin(y[0])
    return np.array([z1, z2])

def eulerstep(t, x, h):
    return ([x[j]+h*ydot(t,x)[j] for j in range(len(x))])

def eulersmethod(Theta0, T, n):
    z = Theta0
    h = T/n
    t = [i*h for i in range(n)]
    theta = [[],[]]
    for i in range(n):
        z = eulerstep(t[i], z, h)
        theta[0].append(z[0])
        theta[1].append(z[1])
    return(t, theta[0], theta[1])

def animate_pendulum(x, y, h):
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(autoscale_on = False, xlim=(-2.2, 2.2), ylim = (-2.2, 2.2))
    ax.grid()
    line = ax.plot([],[], 'o', c='blue', lw=1)
    time_text = ax.text(0.05, 0.9, '', transform = ax.transAxes)

    def animate(i):
        xline = [0, x[1]]
        yline = [0, y[1]]

        line.set_data(xline, yline)
        time_text.set_text(f"time = {i*h:1f}s")
        return line, time_text
    ani = FuncAnimation(
        fig, animate, len(x), interval = h*1000, blit = True, repeat = False
    )
    plt.show()

def min():
    L=2
    T=20
    n=500
    h=T/n
    y_0 = [np.pi/12, 0]

    t, angle, velocity = eulersmethod(y_0, T, n)
    x, y = L*np.sin(angle[:]), -L*np.cos(angle[:])
    animate_pendulum(x, y, h)

min()
ajsxfq5m

ajsxfq5m1#

plot()返回一个行列表,见docs。你可以通过在变量赋值中添加一个逗号来解压列表。

# Liður 2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def ydot(t, y):
    g = 9.81
    l = 1
    z1 = y[1]
    z2 = -g/l*np.sin(y[0])
    return np.array([z1, z2])

def eulerstep(t, x, h):
    return ([x[j]+h*ydot(t,x)[j] for j in range(len(x))])

def eulersmethod(Theta0, T, n):
    z = Theta0
    h = T/n
    t = [i*h for i in range(n)]
    theta = [[],[]]
    for i in range(n):
        z = eulerstep(t[i], z, h)
        theta[0].append(z[0])
        theta[1].append(z[1])
    return(t, theta[0], theta[1])

def animate_pendulum(x, y, h):
    fig = plt.figure(figsize=(8,8))
    ax = fig.add_subplot(autoscale_on = False, xlim=(-2.2, 2.2), ylim = (-2.2, 2.2))
    ax.grid()
    line, = ax.plot([],[], 'o', c='blue', lw=1)
    time_text = ax.text(0.05, 0.9, '', transform = ax.transAxes)

    def animate(i):
        xline = [0, x[1]]
        yline = [0, y[1]]

        line.set_data(xline, yline)
        time_text.set_text(f"time = {i*h:1f}s")
        return line, time_text
    ani = FuncAnimation(
        fig, animate, len(x), interval = h*1000, blit = True, repeat = False
    )
    plt.show()

def min():
    L=2
    T=20
    n=500
    h=T/n
    y_0 = [np.pi/12, 0]

    t, angle, velocity = eulersmethod(y_0, T, n)
    x, y = L*np.sin(angle[:]), -L*np.cos(angle[:])
    animate_pendulum(x, y, h)

min()

相关问题