【发布时间】:2014-01-13 18:56:31
【问题描述】:
我正在尝试实现一个输出动画情节的函数。
如果我以 simple_anim.py(来自 matplotlib 示例)为基础:
"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01) # x-array
line, = ax.plot(x, np.sin(x))
def animate(i):
line.set_ydata(np.sin(x+i/10.0)) # update the data
return line,
#Init only required for blitting to give a clean slate.
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
interval=25, blit=True)
plt.show()
它确实有效。
但是,如果我在函数内关闭此代码(以提供更改参数,并避免为每个可能的参数值创建显式文件):
"""
A simple example of an animated plot
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def a():
fig, ax = plt.subplots()
x = np.arange(0, 2*np.pi, 0.01) # x-array
line, = ax.plot(x, np.sin(x))
def animate(i):
line.set_ydata(np.sin(x+i/10.0)) # update the data
return line,
#Init only required for blitting to give a clean slate.
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
interval=25, blit=True)
plt.show()
然后调用该函数,图形绘图保持白色。事实上,它永远不会进入动画功能。
我知道我遗漏了一些信息,这就是它不起作用的原因。 有人可以给我一些提示吗?
非常感谢,
安德烈斯
【问题讨论】:
-
这两个代码对我来说都很好。也请张贴代码调用
a。
标签: python animation matplotlib