matplotlib.pyplot.draw文件()和matplotlib.pyplot.show文件()有n个

2024-06-30 16:07:19 发布

您现在位置:Python中文网/ 问答频道 /正文

在过去,我可以用matplotlib和for循环来制作简单的动画,但是这已经有一段时间不起作用了。你知道吗

标准答案是必须打开交互模式和/或使用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()中也会出现这种情况)

当我在ipython3.1(使用python3.3.5)中从命令行运行它时,我什么都没有得到。你知道吗

生成图像时,mplot.savefig(filename)行似乎确实起作用。你知道吗

(这可能是Qt4后端的一个bug。)


Tags: importformatplotlibasnpfig动画ax