![fig fig]()
使用 python -i test.py 运行时也是如此,但如果我现在在交互式会话中输入相同的代码>>>fig = plt.figure()>>>title = fig.suptitle("测试_")>>>FuncAnimation(fig, anim)>>>plt.show()一切都按预期进行.我已经关注这个问题很久了,我似乎没有发现任何与此相关的问题或疑问.我在 OS X 上的 python 3.5.2 中使用 matplotlib 2.0.0.这是一个(已知的)错误吗?任何人都知道为什么会发生这种情况或如何解决? 解决方案 来自 动画文档:[..] 保持对实例对象的引用至关重要."因此您需要通过将 FuncAnimation 实例分配给一个变量来使其保持活动状态.将 matplotlib.pyplot 导入为 plt从 matplotlib.animation 导入 FuncAnimation如果 __name__ == '__main__':fig = plt.figure()title = fig.suptitle("测试_")定义动画(i):title.set_text("测试 %d" % i)plt.plot([0,1], [0,1])ani = FuncAnimation(fig, anim)plt.show()有一个正在进行的讨论关于Animation是否应该内部存储与否.If I create a file test.py with the codeimport matplotlib.pyplot as pltfrom matplotlib.animation import FuncAnimationif __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()and try to run it in my command line, using python test.py, I get an empty screen with the title Test _ and without any axes.The same is true when running with python -i test.py, but if I now enter the same code in the interactive session>>> fig = plt.figure()>>> title = fig.suptitle("Test _")>>> FuncAnimation(fig, anim)>>> plt.show()everything just works as expected.I have been looking at this for so long now and I don't seem to find any issues or questions that are related to this. I am using matplotlib 2.0.0 in python 3.5.2 on OS X.Is this a (known) bug? Anyone with ideas why this might be happening or how this could be resolved? 解决方案 From the animation documentation: "[..] it is critical to keep a reference to the instance object."So you need to keep the FuncAnimation instance alive by assigning it to a variable.import matplotlib.pyplot as pltfrom matplotlib.animation import FuncAnimationif __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()There is an ongoing discussion about whether the Animation should be stored internally or not. 这篇关于matplotlib 动画只能在交互式会话中工作的任何原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-18 21:12