0

我在循环中有一个重复的图形调用。因为后端需要继续运行,所以我将绘图拆分到另一个线程中(使用交互模式会锁定图形,因为后端正在使用对 C++ 的子进程调用)。但是,当后端再次出现图形时,这似乎会导致问题。虽然它继续运行,但在第一次之后绘图失败。只要代码正在运行,我就需要它能够继续打开添加的窗口,这样用户就可以离开并稍后再回来,并且仍然可以打开所有图表。如何根据需要调出尽可能多的窗口,并在底层代码完成时将它们保留在那里(窗口喜欢关闭所有 CMD 窗口,第二个代码停止执行)?

import subprocess
import threading
from matplotlib import pyplot as mpl
...
for x in data:
...
   if condition:
...
      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()
 ....
   subprocess.Popen(file) 
4

1 回答 1

0

听起来像一个复杂的设置,只是为了以非阻塞方式运行你的图。交互模式不切芥末吗?

import matplotlib.pyplot as plt

plt.interactive(True)

# complex code which produces multiple figures goes here...

# ... and might do something like 
fig1 = plt.figure()
plt.plot(range(10))
plt.draw()

# ... or this
fig2 = plt.figure()
plt.polar(range(10))
plt.draw()

# once everything is done, we can put in a blocking call
# which will terminate when the last window is closed by the user
plt.show(block=True)
于 2012-07-19T06:57:35.607 回答