Не отображается анимация на Python
Я написал код для анимации маятника, но при запуске анимация просто не отображается, причем код запускается сразу же. Как это исправить? Вот сам код:
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
g = 9.81
class Pendulum:
def __init__(self, sus_x, sus_y, angle_0, length):
self.sus_x = sus_x
self.sus_y = sus_y
self.angle_0 = np.radians(angle_0)
self.length = length
self.angle = self.angle_0
self.velocity = 0
def NextFrame(self, dt):
acceleration = (-1) * g / self.length * np.sin(self.angle)
self.velocity += acceleration * dt
self.angle += self.velocity * dt
def Position(self):
x = self.sus_x + self.length * np.sin(self.angle)
y = self.sus_y + self.length * np.cos(self.angle)
return x, y
pendulum = Pendulum(0.0, 0.0, 30, 10)
fig, ax = plt.subplots()
ax.set_xlim(-12, 12)
ax.set_ylim(-12, 2)
line, = ax.plot([], [], 'o-', lw=2)
def Init():
line.set_data([], [])
return line,
def Animate(frame):
pendulum.NextFrame(0.05)
x, y = pendulum.Position()
line.set_data([pendulum.sus_x, x], [pendulum.sus_y, y])
return line,
ani = animation.FuncAnimation(fig, Animate, init_func=Init, frames=20000, interval=50, blit=True)
plt.show()