(python)matplotlib pyplot show()..阻塞还是不阻止? [英] (python) matplotlib pyplot show() .. blocking or not?

查看:1878
本文介绍了(python)matplotlib pyplot show()..阻塞还是不阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了这个麻烦,一个又一次的 show(),我相信我做错了,但不能肯定'正确'方式做我想要的

I have run into this trouble with show() over and over again, and I'm sure I'm doing something wrong but not sure of the 'correct' way to do what I want.

而且我想要的是在主线程中阻塞一些事情,直到GUI线程中发生事件,这样的事情首次发生: / p>

And [I think] what I want is some way to block in the main thread until an event happens in the GUI thread, something like this works the first time:

from matplotlib import pyplot as p
from scipy import rand

im = (255*rand(480,640)).astype('uint8')
fig = p.figure()
ax = fig.add_subplot(111)
ax.imshow(im)

# just any mutable container for storing a click
s = [-1, -1]

def onclick(event):
  if event.xdata is not None and event.ydata is not None:
    s[0] = event.xdata
    s[1] = event.ydata
    p.close()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
p.show()
print s


$ b $直到 p.close()在事件处理程序中被调用时, p.show()阻止。但是当第二次运行相同的代码时,它会通过 p.show()并打印原始的 s,[-1,-1 ]

the p.show() blocks until p.close() is called in the event handler. But when run the same code the second time, it races past the p.show() and prints that original s, [-1, -1].

我读过有关$ p.show()是否可以多次调用的冲突信息从同一个程序。它似乎被设计为使用一次,只有一次在脚本结束。其他用例似乎打破了 pyplot 某种方式(状态机?)。

I have read conflicting information on whether or not p.show() can or should be called more than once from the same program. It seems it was designed to be used once, and only once at the end of a script. Other use cases seem to break pyplot somehow (state machine?).

我试图使用 p.draw() p.ion() p.ioff() / code>,但无法获得我想要的行为(任何事情都不能正确阻止,或者图块没有在正确的时间出现)。

I've tried to use combinations of p.draw() and p.ion() and p.ioff(), but could not get the behaviour I wanted (either things weren't blocking properly or the plots weren't appearing at the right times).

我也感到困惑的是事件处理程序能够在这里看到 s ,以及这是否是传入/传出信息的糟糕方式。如果我不使用像数组或列表这样的可变容器,我希望由事件处理程序设置的信息将作为局部变量丢失。有没有其他方法我错过了,GUI线程可以传递信号回主线程?有没有办法阻止主要,没有定期轮询或忙碌等待,来自事件处理程序的信号继续之前?

I'm also confused about how the event handler is able to see s at all here, and whether this is a poor way of passing in/out information. If I don't use a mutable container like an array or list, the information I want set by the event handler just gets lost as a local variable. is there some other method I'm missing out on , where the GUI thread can pass signals back to the main thread? is there a way to block in main, without periodic polling or busy waiting , for a signal from the event handler before continuing?

所以我猜最终我的主要问题是:

So I guess ultimately my main question is:

是否有一个整洁的替换为 p.show(),这样做是我想要的(与 p.show()有同样的行为是第一次),或者这种代码需要一个完整的反思/重写?

Is there a neat replacement for p.show(), that does what I want (the same behaviour as p.show() has the first time), or does this kind of code require a complete rethink/rewrite ?

推荐答案

我今天能够解决我的问题。如果任何人有兴趣更改 show()的行为,请继续阅读以下操作:

I was able to resolve my issue today. if anyone else is interested in changing the behaviour of show(), read on for how you can do it:

我注意到在新功能的多个来电显示支持 / a>一部分matplotlib网页:

I noticed this paragraph titled multiple calls to show supported on the what's new part of the matplotlib webpage:


长期的要求是支持多次调用show()。这是困难的,因为在操作系统,用户界面工具包和版本之间难以获得一致的行为。埃里克·费雷(Eric Firing)已经做了大量的工作来合理化展示后端,使所需的行为使展示提高所有新创建的数字,并阻止执行,直到关闭。自上次电话以来,重复呼叫电话应该会提高新创建的数字。埃里克已经对用户界面工具包和他可以访问的版本和平台进行了大量的测试,但是不可能对它们进行测试,所以请向邮件列表和错误跟踪报告问题。

A long standing request is to support multiple calls to show(). This has been difficult because it is hard to get consistent behavior across operating systems, user interface toolkits and versions. Eric Firing has done a lot of work on rationalizing show across backends, with the desired behavior to make show raise all newly created figures and block execution until they are closed. Repeated calls to show should raise newly created figures since the last call. Eric has done a lot of testing on the user interface toolkits and versions and platforms he has access to, but it is not possible to test them all, so please report problems to the mailing list and bug tracker.

这是在$ code> 1.0.1 的新功能中,在刻录版本的突触仍然回到 0.99.3 。我可以从源代码 v1.0.1 下载和构建。额外的包我也需要满足依赖关系是 libfreetype6-dev tk-dev tk8.5-dev tcl8.5-dev python-gtk2-dev

This was in 'what's new' for version 1.0.1, at time of writing the version in synaptic is still back on 0.99.3. I was able to download and build from source v1.0.1. Additional packages I also required to satisfy dependencies were libfreetype6-dev tk-dev tk8.5-dev tcl8.5-dev python-gtk2-dev.

现在使用 matplotlib .__ version__ == 1.0.1 ,以下代码阻止了我的期望: p>

Now with matplotlib.__version__ == 1.0.1 , the following code blocks how I would expect:

import matplotlib.pyplot as p
from scipy import eye
p.imshow(eye(3))
p.show()
print 'a' 
p.imshow(eye(6))
p.show()
print 'b' 
p.imshow(eye(9))
p.show()
print 'c' 

这篇关于(python)matplotlib pyplot show()..阻塞还是不阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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