7

我正在尝试弹出一个情节,以便用户可以确认配件是否有效,但不要挂断整个过程。然而,当窗口出现时,里面从来没有任何东西,它是“无响应”。我怀疑与子进程功能的交互不好,因为这段代码是前端和数据处理,用于在 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()call 并 use mpl.show(),那么情节可以正常工作,但整个过程会一直持续到窗口关闭。我需要在用户查看图表时继续该过程。有没有办法做到这一点?

4

2 回答 2

8

代替 mpl.draw(),尝试:

mpl.pause(0.001)

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

于 2012-07-15T12:28:13.490 回答
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()
于 2012-07-17T20:54:23.590 回答