Python matplotlib动画为什么只能在交互式会话中工作?
如果我用代码创建一个文件Python matplotlib动画为什么只能在交互式会话中工作?,python,macos,animation,matplotlib,Python,Macos,Animation,Matplotlib,如果我用代码创建一个文件test.py import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation if __name__ == '__main__': fig = plt.figure() title = fig.suptitle("Test _") def anim(i): title.set_text("Test %d" % i) p
test.py
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
if __name__ == '__main__':
fig = plt.figure()
title = fig.suptitle("Test _")
def anim(i):
title.set_text("Test %d" % i)
plt.plot([0,1], [0,1])
FuncAnimation(fig, anim)
plt.show()
然后试着在我的命令行中运行它,使用python test.py
,我得到一个标题为test\uu
且没有任何轴的空屏幕
使用python-i test.py运行时也是如此,但是如果我现在在交互式会话中输入相同的代码
>>> fig = plt.figure()
>>> title = fig.suptitle("Test _")
>>> FuncAnimation(fig, anim)
>>> plt.show()
一切正常
我已经看了这么长时间了,我似乎没有发现任何与此相关的问题。我正在OS X上使用python 3.5.2中的matplotlib 2.0.0
这是一个(已知的)错误吗?有人知道为什么会发生这种情况或者如何解决吗?来自:“[…]保持对实例对象的引用是至关重要的。”
因此,需要通过将FuncAnimation实例指定给变量来保持其活动状态
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
if __name__ == '__main__':
fig = plt.figure()
title = fig.suptitle("Test _")
def anim(i):
title.set_text("Test %d" % i)
plt.plot([0,1], [0,1])
ani = FuncAnimation(fig, anim)
plt.show()
关于是否应在内部存储动画
,存在一个问题 我在那里花了很多时间。。。愚蠢的是我没有发现它。谢谢PS:你知道为什么在交互模式下(只是出于好奇)这仍然可以工作吗?你说的交互模式是指IPython控制台?在这种情况下,您就不会失去引用,因为单元格输出保存在内存中。不,这是python解释器的交互模式。解释为什么动画对象在我的示例中没有被垃圾收集。。。