pythonmatplotlibanimationplotmatplotlib-animation

Make animation using Python but give me an error, animation can't appear


I want to plot animation of y=omega*x^2, which omega is -3 into 3 with step size 0.25, x from -4 into 4 with step size 0.001. I'm new studying plot animation in Python. I try to make a code but give me an error as follows and no animation appear.

UserWarning: Animation was deleted without rendering anything. This is most likely not intended. To prevent deletion, assign the Animation to a variable, e.g. `anim`, that exists until you have outputted the Animation using `plt.show()` or `anim.save()`.

Process finished with exit code 0

This is my code:

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

fig = plt.figure()
axis = plt.axes(xlim=(-4, 4), ylim=(-40, 40))
line, = axis.plot([], [], lw=3)

def init():
    line.set_data([], [])
    return line,

def animate(omega):
    x = np.linspace(-4, 4, 8000)
    y = omega*x**2
    line.set_data(x, y)
    return line,

anim = FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)

I don't know what the mistake of the code. How to fix the code such that the animation can appear? Thanks for your help.


Solution

  • As the error message you've provided says, you just need to include plt.show() at the very end of your code, and you will see the animation.