过去我可以使用带有 for 循环的 matplotlib 来制作简单的动画,但现在已经有一段时间没用了。
标准答案是您必须打开交互模式和/或使用matplotlib.pyplot.draw()
强制重绘。这是我的最小工作示例:
import numpy as np
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as mplot
mplot.ion()
fig = mplot.figure(1)
ax = fig.add_subplot(111)
for ii in np.arange(0,10):
x = 200*np.random.rand(30)
ax.plot(x)
mplot.draw()
filename = ("img_%d.png" % ii)
mplot.savefig(filename)
当我在 Interactive Python Editor 中运行它时,我在最后得到一个数字,其中包含所有绘图(这也发生在 mplot.show()
中)
当我从命令行在 IPython 3.1(使用 Python 3.3.5)中运行它时,我什么也没得到。
随着图像的生成,mplot.savefig(filename)
行似乎确实有效。
(这可能是 Qt4 后端的一个错误。)
最佳答案
尝试删除行matplotlib.use('Qt4Agg')
。对我有用。也适用于matplotlib.use('TkAgg')
。所以这是一个后端问题。有another way做动画。
关于python - matplotlib.pyplot.draw() 和 matplotlib.pyplot.show() 没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444838/