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

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

问题描述

最近几天我一直在玩 Numpy 和 matplotlib.我在尝试使 matplotlib 绘图成为一个函数而不阻止执行时遇到问题.我知道这里已经有很多关于 SO 的帖子在问类似的问题,我已经在谷歌上搜索了很多,但都没有成功.

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. 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)(或任何你想要的时间).需要 pause 因为 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天全站免登陆