简单的Python / Matplotlib动画显示:空图 - 为什么?

时间:2018-01-15 08:50:47

标签: python animation matplotlib

我只是想了解matplotlib的动画是如何工作的。为此,我查找了示例并进行了复制粘贴,只是为了让它们运行并理解它。

不幸的是,即使matplotlib网站上的示例也只显示一个空图(例如https://matplotlib.org/examples/animation/animate_decay.html)。

轴,网格,背景 - 外翻,没有图形或动画。

我用过,例如来自网站https://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/的此代码:

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


# First set up the figure, the axis, and the plot element we want to animate
fig5 = plt.figure(5, figsize=(15,15))
ax5 = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, =ax5.plot([], [], lw=2)

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    print(x,y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig5, animate, init_func=init,
                           frames=200, interval=200, blit=True)
plt.show()

这产生了 - 但为什么呢? empty graph

编辑: 我不知道我做错了什么,但这里给出的解决方案或代码示例都没有Animation in iPython notebook

这有什么作用:

 from matplotlib import rc, animation
 rc('animation', html='html5')

然后使用

anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=N, interval=20, blit=True)
anim

这会产生一段9秒长的短视频,在我按下停止按钮之前,该视频会自动运行并重复播放。

0 个答案:

没有答案