Matplotlib离子()和子过程

Ell*_*iot 7 python subprocess matplotlib interactive-mode

我试图弹出一个情节,以便用户可以确认一个合适的工作,但不会挂断整个过程.但是,当窗口出现时,它中永远不存在任何内容,并且它是"无响应".我怀疑与子进程功能的交互不良,因为这个代码是前端的,并且模拟的数据处理是在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)
Run Code Online (Sandbox Code Playgroud)

以下子进程确实打开.如果我删除了ion()呼叫并使用mpl.show(),那么绘图工作正常,但整个过程都会保持到窗口关闭为止.我需要在用户查看图表时继续该过程.有没有办法做到这一点?

Nie*_*els 7

而不是mpl.draw(),尝试:

mpl.pause(0.001)
Run Code Online (Sandbox Code Playgroud)

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


Ell*_*iot 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()
Run Code Online (Sandbox Code Playgroud)