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

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

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

但是如果我在Spyder中做同样的事情,一旦我要求用户输入,Matplotlib窗口会冻结,所以我无法与之交互 . 当提示显示时,我需要与绘图进行交互 .

在Spyder和普通控制台中,matplotlib.get_backend()返回'Qt4Agg'

Edit: 为了澄清,我有matplotlib设置在它自己的窗口中显示,而不是作为PNG嵌入 . (我必须设置Backend:Automatic最初才能获得此行为)

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

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

import matplotlib.pyplot as pl

def anomaly_selection(indexes, fig, ax):
    selected = []

    for i in range(0, len(indexes)):
        index = indexes[i]
        ax.set_xlim(index-100, index+100)
        ax.autoscale_view()
        fig.canvas.draw()
        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":
                return selected
            elif response == "n":
                break

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

sel = anomaly_selection([100, 1000, 53000, 4300], fig, ax[0])

Lots of Edit: 我认为这是input()阻塞Qt的问题 . 如果这个问题没有引起关注,我的解决方法是构建一个Qt窗口,其中嵌入了Matplotlib图,并通过窗口获取键盘输入 .

回答(2)

3 years ago

经过更多的挖掘后,我得出的结论是,你应该制作一个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()

3 years ago

知道比我更好的人,如果可能的话请发一个答案 . 我对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