Matplotlib冻结当Spyder中使用input()时

时间:2016-01-22 03:38:51

标签: python matplotlib ipython spyder

Windows 7.如果我在命令行打开一个普通的ipython终端,我可以输入:

<div class="section-ad group">
<div class="col span_1_of_2">
<img src="https://cdn.shopify.com/s/files/1/0786/5107/files/main-sale-large.jpg?11274335160121521292" />
</div>
<div class="col span_1_of_2">
<img src="https://cdn.shopify.com/s/files/1/0786/5107/files/main-plain-x450_ab05c8e5-7269-4d98-b510-8ba5127db9c4.jpg?13010707129353802904" />
</div>
</div>

但如果我在Spyder中做同样的事情,一旦我要求用户输入,Matplotlib窗口就会冻结,所以我无法与它进行交互。当提示出现时,我需要与情节互动。

在Spyder和普通控制台中,matplotlib.get_backend()返回&#39; Qt4Agg&#39;

编辑为了澄清,我将matplotlib设置在自己的窗口中,而不是作为PNG嵌入。 (我必须设置Backend:Automatic最初才能获得此行为)

另外,在Spyder中,情节会在plt.plot()之后立即打开。在常规控制台中,它仅在plt.show()之后打开。另外,如果我在Spyder中输入input()后按Ctrl-C,整个控制台会意外挂起。比。在IPython中,它只是引发KeyboardInterrupt并将控制权返回给控制台。

编辑:更完整的示例:在IPython控制台中工作,而不是在Spyder中工作(冻结)。想根据用户输入移动情节。

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4, 5])
plt.show(block=False)
input("Hello ")

很多编辑:我认为这是input()阻止Qt的问题。如果这个问题没有引起关注,我的解决方法是构建一个Qt窗口,其中嵌入了Matplotlib图,并通过窗口获得键盘输入。

2 个答案:

答案 0 :(得分:3)

如果有人比我知道的更多,请尽可能回复。我对Python / Scipy / Spyder

知之甚少

这是我编写的一个kludgy模块,可以防止Matplotlib窗口在Spyder下输入()处于挂起状态时冻结。

您必须事先致电prompt_hack.start()并在prompt_hack.finish()之后致电,并将input()替换为prompt_hack.input()

<强> prompt_hack.py

import matplotlib.pyplot
import time
import threading

# Super Hacky Way of Getting input() to work in Spyder with Matplotlib open
# No efforts made towards thread saftey!

prompt = False
promptText = ""
done = False
waiting = False
response = ""

regular_input = input

def threadfunc():
    global prompt
    global done
    global waiting
    global response

    while not done:   
        if prompt:   
            prompt = False
            response = regular_input(promptText)
            waiting = True
        time.sleep(0.1)

def input(text):
    global waiting
    global prompt
    global promptText

    promptText = text
    prompt = True

    while not waiting:
        matplotlib.pyplot.pause(0.01)
    waiting = False

    return response

def start():
    thread = threading.Thread(target = threadfunc)
    thread.start()

def finish():
    global done
    done = True

答案 1 :(得分:1)

经过更多的挖掘后,我得出的结论是你应该制作一个GUI。我建议你使用PySide或PyQt。为了使matplotlib具有图形窗口,它运行一个事件循环。任何单击或鼠标移动都会触发一个事件,触发图形部分执行某些操作。脚本编写的问题是每个代码都是顶级的;它表明代码是按顺序运行的。

当您手动将代码输入ipython控制台时,它可以正常工作!这是因为ipython已经启动了一个GUI事件循环。您调用的每个命令都在事件循环中处理,允许其他事件发生。

您应该创建一个GUI并将该GUI后端声明为相同的matplotlib后端。如果你有一个按钮点击触发anomaly_selection函数,那么该函数在一个单独的线程中运行,并且应该允许你仍然在GUI中进行交互。

通过大量的摆弄和移动你调用fucntions的方式,你可以让thread_input函数工作。

幸运的是,PySide和PyQt允许您手动调用处理GUI事件。我添加了一个方法,要求在单独的线程中输入并循环等待结果。在等待时,它告诉GUI处理事件。如果安装了PySide(或PyQt)并将其用作matplotlib的后端,return_input方法将有用。

import threading

def _get_input(msg, func):
    """Get input and run the function."""
    value = input(msg)
    if func is not None:
        func(value)
    return value
# end _get_input

def thread_input(msg="", func=None):
    """Collect input from the user without blocking. Call the given function when the input has been received.

    Args:
        msg (str): Message to tell the user.
        func (function): Callback function that will be called when the user gives input.
    """
    th = threading.Thread(target=_get_input, args=(msg, func))
    th.daemon = True
    th.start()
# end thread_input

def return_input(msg=""):
    """Run the input method in a separate thread, and return the input."""
    results = []
    th = threading.Thread(target=_store_input, args=(msg, results))
    th.daemon = True
    th.start()
    while len(results) == 0:
        QtGui.qApp.processEvents()
        time.sleep(0.1)

    return results[0]
# end return_input

if __name__ == "__main__":

    stop = [False]
    def stop_print(value):
        print(repr(value))
        if value == "q":
            stop[0] = True
            return
        thread_input("Enter value:", stop_print)

    thread_input("Enter value:", stop_print)
    add = 0
    while True:
        add += 1
        if stop[0]:
            break

    print("Total value:", add)

此代码似乎对我有用。虽然它确实给了我一些ipython内核的问题。

from matplotlib import pyplot as pl

import threading


def anomaly_selection(selected, indexes, fig, ax):
    for i in range(0, len(indexes)):
        index = indexes[i]
        ax.set_xlim(index-100, index+100)
        ax.autoscale_view()
        #fig.canvas.draw_idle() # Do not need because of pause
        print("[%d/%d] Index %d " % (i, len(indexes), index), end="")
        while True:
            response = input("Particle? ")
            if response == "y":
                selected.append(index)
                break
            elif response == "x":
                selected[0] = True
                return selected
            elif response == "n":
                break

    selected[0] = True
    return selected


fig, ax = pl.subplots(2, sharex=True)
ax[0].plot([1, 2, 3, 4, 5]) # just pretend data
pl.show(block=False)

sel = [False]
th = threading.Thread(target=anomaly_selection, args=(sel, [100, 1000, 53000, 4300], fig, ax[0]))
th.start()
#sel = anomaly_selection([100, 1000, 53000, 4300], fig, ax[0])


while not sel[0]:
    pl.pause(1)
th.join()