0
from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
def animate(i):
    line.set_ydata(np.sin(x + i/10.0))
    return line,
def init():
    line.set_ydata(np.sin(x))
    return line,
ani = animation.FuncAnimation(fig=fig,
                              func=animate,
                              frames=100,
                              init_func=init,
                              interval=20,
                              blit=False)
plt.show()

I coded in jupyter Notebook,matplotlib's version is 2.2.0 and python's is 2.7. I tried to display a animation,but the output is only the first frame,a static picture. I cannot find the error.

This is the static picture:

enter image description here

LinFelix
  • 1,026
  • 1
  • 13
  • 23
LiXieyun
  • 21
  • 3
  • alternatively to the answer provided below, you can use the "notebook" backend. remplace `%matplotlib inline` by `%matplotlib notebook` – Diziet Asahi Dec 18 '18 at 08:25

1 Answers1

1

Jupyter notebooks output pngs which cannot be animated. You can use Javascript to animate a plot in a notebook.

from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np
from IPython.display import HTML


fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))
def animate(i):
    line.set_ydata(np.sin(x + i/10.0))
    return line,
def init():
    line.set_ydata(np.sin(x))
    return line,
ani = animation.FuncAnimation(fig=fig,
                              func=animate,
                              frames=100,
                              init_func=init,
                              interval=20,
                              blit=False)

HTML(ani.to_jshtml())
Brandon Schabell
  • 1,735
  • 2
  • 10
  • 21