Matplotlib ion()和子过程

问题描述 投票:7回答:2

我正在尝试弹出一个图,以便用户可以确认拟合是否有效,但不会挂断整个过程。但是,当窗口出现时,窗口中什么也没有,它是“无响应”。我怀疑与子流程功能之间的交互不良,因为此代码是前端代码,并且数据的处理都在C ++中运行。

import subprocess
import numpy as np
from matplotlib import pyplot as mpl
...
mpl.ion()
fig = mpl.figure()
ax = fig.add_subplot(1,1,1)
ax.grid(True)
ax.plot(x, y, 'g')
ax.scatter(X, Y, c='b')
ax.scatter(min_tilt, min_energy, c='r')
mpl.draw()
...
subprocess.call(prog)

以下子过程确实打开。如果我删除了ion()调用并使用了mpl.show(),则绘图工作正常,但整个过程将一直持续到关闭窗口。当用户查看图表时,我需要继续执行该过程。有办法吗?

python matplotlib subprocess
2个回答
7
投票

代替mpl.draw(),请尝试:

mpl.pause(0.001)

使用matplotlib交互模式ion()时。请注意,这仅适用于matplotlib 1.1.1 RC或更高版本。


1
投票

这可能是多余的,但是由于没有人有更好的解决方案,因此我去了线程模块,它可以正常工作。如果有人有更简单的方法,请告诉我。

import subprocess
import threading
from matplotlib import pyplot as mpl
...
class Graph(threading.Thread):
   def __init__(self,X,Y,min_tilt, min_energy):
       self.X = X
       self.Y = Y
       self.min_tilt = min_tilt
       self.min_energy = min_energy
       threading.Thread.__init__(self)

   def run(self):
       X = self.X
       Y = self.Y
       dx = (X.max()-X.min())/30.0
       x = np.arange(X.min(),X.max()+dx,dx)
       y = quad(x,fit)
       fig = mpl.figure()
       ax = fig.add_subplot(1,1,1)
       ax.grid(True)
       ax.plot(x, y, 'g')
       ax.scatter(X, Y, c='b')
       ax.scatter(self.min_tilt, self.min_energy, c='r')
       mpl.show()
thread = Graph(X,Y,min_tilt,min_energy)
thread.start()
© www.soinside.com 2019 - 2024. All rights reserved.