Matplotlib 动画要么在几帧后冻结,要么就不起作用 [英] Matplotlib animation either freezes after a few frames or just doesn't work

查看:22
本文介绍了Matplotlib 动画要么在几帧后冻结,要么就不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几个小时来让这个简单的脚本工作,但我所做的一切似乎都没有帮助.这是对来自 Matplotlib 网站的最基本的动画绘图示例代码的轻微修改,它应该只显示几帧噪声(顺便说一句,我对他们网站上未修改的代码也有同样的问题).

I've been trying for hours to get this simple script working, but nothing I do seems to help. It's a slight modification of the most basic animated plot sample code from the Matplotlib website, that should just show a few frames of noise (I have the same issue with the unmodified code from their website BTW).

在我的带有 TkAgg 后端的计算机上,我在绘图窗口冻结之前获得了大约 20 帧(共 60 帧).使用 Qt4Agg 我只是得到一个冻结的黑色窗口,并且根本没有绘制任何帧.我尝试了不同 NumPy、PyQt、Python 和 Matplotlib 版本的多种组合,但总是得到相同的结果.

On my computer with the TkAgg backend I get about 20 frames (out of 60) before the plot window freezes. With Qt4Agg I just get a frozen, black window and no frames at all are plotted. I've tried multiple combinations of different NumPy, PyQt, Python and Matplotlib versions, but always get the same result.

请告诉我这是否适合您,或者是否有任何问题.我很确定这在过去确实有效,所以我认为这可能是 Windows 问题或与 ion() 相关的问题.

Please let me know if this works for you or if anything looks wrong. I'm pretty sure this did work in the past, so I'm thinking it may be a Windows issue or something related to ion().

仅供参考,我使用的是 Windows 7(32 位)并且我已经使用 Python 2.6/2.7、MPL 1.0.0/0.9.9.8、PyQt 4.6/4.7、Numpy 1.4/1.5b 进行了测试.

FYI I'm using Windows 7 (32 bit) and I've tested with Python 2.6/2.7, MPL 1.0.0/0.9.9.8, PyQt 4.6/4.7, Numpy 1.4/1.5b.

import matplotlib
matplotlib.use('TkAgg') # Qt4Agg gives an empty, black window
from pylab import *
import time

ion()
hold(False)

# create initial plot
z = zeros(10)
line, = plot(z)
ylim(-3, 3)

for i in range(60):
    print 'frame:', i

    d = randn(10)
    line.set_ydata(d)

    draw()
    time.sleep(10e-3)

这个更简单的版本在前几帧后也会冻结:

This simpler version also freezes after the first couple frames:

from pylab import *

ion()
hold(False)

for i in range(40):
    plot(randn(10))
    draw()

show()

谢谢!

编辑:这些人似乎和我有同样或相似的问题:

EDIT: These people seem to be having the same or a similar problem as me:

  • mail-archive.com/matplotlib-users@lists.sourceforge.net/msg10844.html
  • stackoverflow.com/questions/2604119/matplotlib-pyplot-pylab-not-updating-figure-while-isinteractive-using-ipython
  • mail-archive.com/matplotlib-users@lists.sourceforge.net/msg01283.html

看起来他们中的任何一个都无法修复它:(

Doesn't look like any of them were able to fix it either :(

推荐答案

通常,GUI 框架需要拥有"程序的主循环.坐在一个带有睡眠的紧密循环中以延迟迭代通常会破坏"GUI 应用程序(您的问题描述与这些方面的典型破坏一致).matplotlib 开发人员可能已经实现了一些幕后逻辑,以防止某些工具包发生这些锁定,但是稍微重构您的程序应该可以消除主循环所有权成为问题的任何可能性(这很可能是我思考).matplotlib animation wiki 还建议使用本机事件循环来处理任何可能不重要的事情为此)

Typically, GUI frameworks need to 'own' the main loop of the program. Sitting in a tight loop with sleeps to delay iterations will usually 'break' GUI applications (your problem description is consistent with typical breakage along these lines). It's possible that the matplotlib devs have implemented some behind the scenes logic to pevent these lockups from happening for certain toolkits but restructuring your program slightly should eliminate any chance of mainloop ownership being the problem (which is very likely I think). The matplotlib animation wiki also suggests using native event loops for anything nontrivial (probably for this reason)

我建议您使用 GUI 工具包在一定延迟后安排函数调用,而不是坐在循环中睡眠.

Rather than sitting in a loop with sleeps, I suggest that, instead, you use the GUI toolkit to schedule a function call after a certain delay.

def update_function():
    # do frame calculation here

refresh_timer = QtCore.QTimer()
QtCore.QObject.connect( refresh_timer, QtCore.SIGNAL('timeout()'), update_function )
refresh_timer.start( 1.0 / 30 ) # have update_function called at 30Hz

查看 matplotlib 文档表明可能可以在本地使用他们的 API,但仅通过快速搜索我找不到任何好的示例.

Looking at the matplotlib documentation suggests that it may be possible to use their API natively but I couldn't find any good examples using just a quick search.

这篇关于Matplotlib 动画要么在几帧后冻结,要么就不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆