2016-11-28 69 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