2016-11-28 35 views
2

好了,所以作为问题建议,我追逐的最好方法创建一个情节:优化Matplotlib情节,可动态更新并保持互动

1)独立于主程序/脚本,因此在更改情节会对主程序/脚本

2)可通过matplotlib的默认GUI或其他自定义GUI

现在进行交互,我有那么无不良(锁定或其他)的影响远达上述标准:

1)使用多处理模块及其多处理队列将X,Y信息传递给一个单独的进程,该进程附加到当前的X,Y数据并刷新绘图。 这基本上解决了标准1.

2)确保matplotlib的交互模式被接通(离子()),无限while True:循环检查上述队列和如果没有X,Y的信息,允许的GUI事件的处理通过pyplot.pause(interval in seconds)

为了简化事情,下面是我的代码来接受传入的X,Y数据并以伪代码绘制它。此代码在不同的进程中运行,队列为其提供X,Y数据。

from matplotlib import pyplot 

def run(self): 

    p = pyplot 

     while True: 
      if there's nothing in the Queue: 
       redraw and allow GUI events (p.pause(0.0001)) 
       continue 
      else: 
       command = get X,Y info from Queue 

       if command equals X,Y data: 

        update plots x data (axes.set_ydata()) 
        update plots y data (axes.set_ydata()) 

        update axis data limits (axes.relim()) 
        update axis view limits (axes.autoscale_view()) 

        redraw and allow GUI events i.e. interaction for 0.0001 seconds (p.pause(0.0001)) 

       elif if command equals the "exit loop" value: 
        break 

     turn of interactive mode (p.ioff()) 
     keep plot open and block process until closed by user (p.show()) 

那么上面可以优化以增加与图形GUI的交互性吗?可能通过允许GUI事件独立于正在更新的图而被服务?这种方法变得更慢更更新,它必须做的(也就是说,如果有在同一人物更地块更新)

任何澄清,只问:)

回答

0

我会通过通过X,Y数据队列(如已经计划的那样),但会将get-command和block-argument一起使用。这样,你的绘图功能就会阻塞,直到队列中有元素。如果没有,则该功能暂停,并且可以处理应用程序中的所有其他事件。

类似的东西:

def process_data(inqueue): 
    #create your plot 

    for infile in iter(inqueue.get(block=True), "STOP"): 
     #update plot until inqueue.get returns "STOP" 
     #waits for elements in the queue enter code here 

def main(): 
    inqueue = Queue() 
    process = Process(target=process_data, args=(inqueue,) 
    #can be stopped by putting "STOP" via inqueue.put into the queue 
    process.start() 
    #put some data in the queue 
+0

感谢您的评论的伴侣。如果Queue中没有项目,那么在队列解锁之前是否锁定Figure的GUI?这就是我理解队列和他们阻止无论如何haha – Sighonide

+0

我不知道。我会假设GUI在其他地方处理,而更新功能块。但是,我只是测试它.. – RaJa