Python pyplot在计算时保持窗口活动

Python pyplot在计算时保持窗口活动,python,matplotlib,Python,Matplotlib,我想让所有pyplot窗口保持活动状态,直到代码本身终止,但不阻塞。 现在,我要么使用 plt.show() 等我关上它再继续 或使用 plt.draw() plt.pause(0.05) 由于某种奇怪的原因,需要暂停的地方,比如没有暂停的地方,窗口永远不会被画出来。在这种情况下,窗口在暂停时间后冻结(通过将其设置为更长时间进行检查) 我想要的是类似于matlab的行为,代码将继续执行脚本,直到脚本结束(等待用户点击enter)。 按enter键后,所有内容都应关闭,就像plt.close(

我想让所有pyplot窗口保持活动状态,直到代码本身终止,但不阻塞。 现在,我要么使用

plt.show()
等我关上它再继续

或使用

plt.draw()
plt.pause(0.05)
由于某种奇怪的原因,需要暂停的地方,比如没有暂停的地方,窗口永远不会被画出来。在这种情况下,窗口在暂停时间后冻结(通过将其设置为更长时间进行检查)

我想要的是类似于matlab的行为,代码将继续执行脚本,直到脚本结束(等待用户点击enter)。 按enter键后,所有内容都应关闭,就像plt.close('all'),一样,但我希望窗口保持活动状态,即-调整大小、根据请求保存并使用数据光标-就像matlab绘图一样,无需关闭所有窗口

一个完整的例子来说明我的问题:

from matplotlib import pyplot as plt
from matplotlib import image as mpimg

img = mpimg.imread("/path/to/image.jpg")
plt.imshow(img)
plt.draw()
plt.pause(05.05) #window works like it should, but execution of rest of the code is frozen

input("hit enter") #the window is dead at this point
plt.close('all')
我看到过这样做的建议():

提到它是特定于linux的,但是因为我运行linux机器不是问题,但是我不知道这里到底发生了什么,也不知道在代码执行过程中多次使用它会发生什么


等待绘图直到结束并不是一个真正的选项,因为第一次和最后一次之间的时间相当长,我更喜欢在早期看到所选的数据产生了不好的结果,而不是运行所有的处理来找出结果。

原则上,您可以使用
plt.show(block=False)
显示绘图后继续执行代码。您可能希望定期更新绘图,使其保持活动状态-这是通过
plt.pause(t)
完成的,其中
t
可以很短(
0.000001
)或相对较长(
1

因为这个想法可能只是让一些模拟在背景中运行,而只是偶尔在情节中显示一些中间结果;由于您不想让绘图干扰模拟,因此可以使用
线程化
。在主线程中,您有一个带有事件循环的绘图和一个每隔一段时间打印一次结果的循环。在另一个线程中有模拟

import numpy as np
import threading
import time
import matplotlib.pyplot as plt

class Worker(object):
    def __init__(self):
        super(Worker, self).__init__()
        self.x = np.linspace(0,1,1000)
        self.y = np.zeros_like(self.x)

    def simulate(self):
        for i in range(15):
            self.y += np.random.randn(1000)
            self.y -= self.y.min()
            time.sleep(1)  # simulate some much longer simulation time
        print("Simulation finished")

w = Worker()
thread = threading.Thread(target=w.simulate)
thread.start()

fig, ax = plt.subplots()
line, = ax.plot(w.x,w.y)

plt.show(block=False)

while thread.is_alive():
    line.set_data(w.x, w.y)
    ax.set_ylim(w.y.min(), w.y.max())
    plt.pause(0.05)

plt.show()

像这样的问题很多,答案总是一样的:
plt.show
是指在脚本结束时只调用一次,以显示一些输出。有一些特殊情况下存在变通办法,例如,您可能希望在冻结的窗口中生活(这仍然允许您在早期阶段查看数据)。我询问是否存在变通办法或发生了新情况(新功能或其他),由于最热门的搜索结果问题目前已有4年零7个月的历史。第二个选项看起来很有希望,我将尝试一下,并写下它是如何进行的,但感谢您的建议!需要一点技巧来掌握一些情节,但我认为这是正确的方法,谢谢。可惜没有内置的方式,但却无能为力
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,1,1000)
y = np.zeros_like(x)

fig, ax = plt.subplots()
line, = ax.plot(x,y)

plt.show(block=False)

for i  in range(15):
    y += np.random.randn(1000)
    y -= y.min()
    line.set_data(x,y)
    ax.set_ylim(y.min(), y.max())
    plt.pause(1)

line.set_data(x,y)
print("Simulation finished")
plt.show()
import numpy as np
import threading
import time
import matplotlib.pyplot as plt

class Worker(object):
    def __init__(self):
        super(Worker, self).__init__()
        self.x = np.linspace(0,1,1000)
        self.y = np.zeros_like(self.x)

    def simulate(self):
        for i in range(15):
            self.y += np.random.randn(1000)
            self.y -= self.y.min()
            time.sleep(1)  # simulate some much longer simulation time
        print("Simulation finished")

w = Worker()
thread = threading.Thread(target=w.simulate)
thread.start()

fig, ax = plt.subplots()
line, = ax.plot(w.x,w.y)

plt.show(block=False)

while thread.is_alive():
    line.set_data(w.x, w.y)
    ax.set_ylim(w.y.min(), w.y.max())
    plt.pause(0.05)

plt.show()