Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'FuncAnimation' object has no attribute '_resize_id'

I am trying to plot a single pendulum using Eulers method and with given theta values and formula in python but I am getting an Attribute error on FuncAnimation saying 'FuncAnimation' object has no attribute '_resize_id'. Does anyone know what I'm doing wrong here?

# 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()
like image 717
Óli Jón Ólason Avatar asked Feb 06 '26 06:02

Óli Jón Ólason


1 Answers

Axes.plot() returns a list of lines, see the docs. You Can unpack the list by adding a comma at the variable assignment. This code runs on my machine:

# 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()
like image 104
hanf_lolo Avatar answered Feb 09 '26 01:02

hanf_lolo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!