我試圖實現一個輸出動畫圖的函數。python matplotlib:無法從函數內調用FuncAnimation
如果我採取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'的代碼。 – Nabla