使用Matplotlib以非阻塞方式进行绘图 [英] Plotting in a non-blocking way with Matplotlib

查看:289
本文介绍了使用Matplotlib以非阻塞方式进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天我一直在与Numpy和matplotlib一起玩.我在尝试使matplotlib绘制函数而不阻止执行时遇到问题.我知道这里已经有很多线程在问类似的问题,并且我已经在Google上搜索了很多,但是还没有成功完成这项工作.

I have been playing with Numpy and matplotlib in the last few days. I am having problems trying to make matplotlib plot a function without blocking execution. I know there are already many threads here on SO asking similar questions, and I 've googled quite a lot but haven't managed to make this work.

我曾尝试按照某些人的建议使用show(block = False),但我得到的只是一个冻结的窗口.如果我简单地调用show(),则将正确绘制结果,但执行将被阻塞,直到关闭窗口为止.从我读过的其他线程中,我怀疑show(block = False)是否起作用取决于后端.这样对吗?我的后端是Qt4Agg.您能否看一下我的代码,并告诉我是否看到错误?这是我的代码.感谢您的帮助.

I have tried using show(block=False) as some people suggest, but all I get is a frozen window. If I simply call show(), the result is plotted properly but execution is blocked until the window is closed. From other threads I 've read, I suspect that whether show(block=False) works or not depends on the backend. Is this correct? My back end is Qt4Agg. Could you have a look at my code and tell me if you see something wrong? Here is my code. Thanks for any help.

from math import *
from matplotlib import pyplot as plt
print plt.get_backend()



def main():
    x = range(-50, 51, 1)
    for pow in range(1,5):   # plot x^1, x^2, ..., x^4

        y = [Xi**pow for Xi in x]
        print y

        plt.plot(x, y)
        plt.draw()
        #plt.show()             #this plots correctly, but blocks execution.
        plt.show(block=False)   #this creates an empty frozen window.
        _ = raw_input("Press [enter] to continue.")


if __name__ == '__main__':
    main()

PS.我忘了说,我想在每次绘制某些东西时都更新现有窗口,而不是创建一个新窗口.

PS. I forgot to say that I would like to update the existing window every time I plot something, instead of creating a new one.

推荐答案

我花了很长时间寻找解决方案,并发现

I spent a long time looking for solutions, and found this answer.

看起来,为了获得您(和我)想要的东西,您需要组合plt.ion()plt.show()(不适用于block=False)和最重要的是plt.pause(.001)(或任何时间)你要).需要暂停是因为GUI事件在主代码处于休眠状态时发生,包括绘画.这很可能是通过从休眠线程中获取时间来实现的,所以IDE可能会为此而烦恼-我不知道.

It looks like, in order to get what you (and I) want, you need the combination of plt.ion(), plt.show() (not with block=False) and, most importantly, plt.pause(.001) (or whatever time you want). The pause is needed because the GUI events happen while the main code is sleeping, including drawing. It's possible that this is implemented by picking up time from a sleeping thread, so maybe IDEs mess with that—I don't know.

这是在python 3.5上对我有用的实现:

Here's an implementation that works for me on python 3.5:

import numpy as np
from matplotlib import pyplot as plt

def main():
    plt.axis([-50,50,0,10000])
    plt.ion()
    plt.show()

    x = np.arange(-50, 51)
    for pow in range(1,5):   # plot x^1, x^2, ..., x^4
        y = [Xi**pow for Xi in x]
        plt.plot(x, y)
        plt.draw()
        plt.pause(0.001)
        input("Press [enter] to continue.")

if __name__ == '__main__':
    main()

这篇关于使用Matplotlib以非阻塞方式进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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