Matplotlib动画或者几帧后,冻结或只是不工作 [英] Matplotlib animation either freezes after a few frames or just doesn't work

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

问题描述

我一直在尝试了几个小时让这个简单的脚本工作,但没有我这样做似乎帮助。这是最基本的动画情节样品$ C $从Matplotlib c网站的轻微的修改,应该只显示噪音几帧(我有从BTW他们的网站未修改code同样的问题)。

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.

请让我知道,如果这对你的作品,或是否有任何错误。我是pretty肯定这的确在过去的工作,所以我想它可能是一个Windows的问题或与离子的东西()

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开发者已经实施了一些场景的逻辑背后的发生某些工具包,但略有调整你的程序应该消除主循环所有权是这个问题的任何机会(这是的非常的可能我pevent这些禁售认为)。该matplotlib <一个href=\"http://www.scipy.org/Cookbook/Matplotlib/Animations#head-2aa776d1bda83e67bff6b0eafd1a19cc870d6648\"相对=nofollow>动画维基还建议使用原生事件对循环平凡的东西(可能是这个原因)

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天全站免登陆