Matplotlib ion()和子过程

发布于 2021-01-29 14:11:27

我正在尝试弹出一个图,以便用户可以确认拟合是否起作用,但不会挂断这样做的整个过程。但是,当窗口出现时,窗口中什么也没有,它是“无响应”。我怀疑与子流程功能之间的交互不良,因为此代码是前端代码,并且数据的处理都在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(),则绘图可以正常工作,但是整个过程将一直持续到关闭窗口。当用户查看图形时,我需要继续执行该过程。有没有办法做到这一点?

关注者
0
被浏览
224
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    这可能是徒劳的,但是由于没有人有更好的解决方案,因此我去了线程模块,它起作用了。如果有人有更简单的方法来进行此操作,请告诉我。

    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()
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看