python - matplotlib.pyplot.draw() 和 matplotlib.pyplot.show() 没有效果

标签 python python-3.x numpy matplotlib

过去我可以使用带有 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/

相关文章:

python - 使用 df.dropna() 返回 NoneType 对象

python-3.x - Pandas 和单元测试之间的冲突?

python - 哪种方法可以更快地替换字符串中最后一次出现的子字符串?

python - 如何迭代/循环列表并引用前 (n-1) 个元素?

python - 使用 numpy 平方值给出负数

python - 使用 GEKKO 模拟具有巨大阵列的状态空间方程

python - 在 Tensorflow 中重新分配后,如何防止此变量从 Variable 转换为 Tensor?

python - 在Python中创建时间序列数据

python - Python 字符串中的 u'\ufeff'

python - 在 Windows 8.1、7 Enterprise 和 7 Home Edition 下安装 numpy 期间 pip 的编译器问题