2011-02-17 54 views
1

我的問題是: 我在PyGTK應用程序中有Matplotlib數字,每隔幾秒就會更新一次。我添加了abbility以將圖形保存爲PNG文件。在調用figure.savefig(filename, other parameters)後,我的應用程序中的數字停止更新。python matplotlib savefig後更新數字

圖初始化階段:

# setup matplotlib stuff on empty space in vbox4 
    figure = Figure() 
    canvas = FigureCanvasGTK(figure) # a gtk.DrawingArea 
    canvas.show() 
    self.win.get_widget('vbox4').pack_start(canvas, True, True) # this will be aded to last place 
    self.win.get_widget('vbox4').reorder_child(canvas, 1) #place plot to space where it should be 

圖被更新這種方式(這稱爲單獨的線程每幾秒鐘):

def _updateGraph(self, fig, x, x1, y): 
    #Various calculations done here 

    fig.clf()#repaint plot: delete current and formate a new one 
    axis = fig.add_subplot(111) 
    #axis.set_axis_off() 
    axis.grid(True) 
#remove ticks and labels 
    axis.get_xaxis().set_ticks_position("none") 
    for i in range(len(axis.get_xticklabels())): axis.get_xticklabels()[i].set_visible(False) 
    axis.get_yaxis().set_ticks_position("none") 
    axis.plot(numpy.array(x),numpy.array(y)/(1.0**1), "k-" ,alpha=.2) 
    axis.set_title('myTitle') 
    fig.autofmt_xdate() 
    fig.canvas.draw() 

一切正常。但打完電話後:

figure.savefig(fileName, bbox_inches='tight', pad_inches=0.05) 

文件已保存,但我的屏幕上的數字停止更新

任何想法如何保存圖形到磁盤,仍然能夠更新我的屏幕上的圖?

+1

一個獨立的例子可能幫助。也就是說,沒有人知道「gui」是什麼,savefig如何從這些代碼片段中調用等等。當然,你會想把它削減到不是太多線條但顯示問題的東西。 –

+0

謝謝你試圖幫助我。是的,我知道這段代碼是無用的,不能作爲獨立運行。圖保存方法由按鈕按下事件調用。 'gui.w()。get(「figureMain」)'是我對個人使用的對象的個人哀悼。它只是一個變量。我將嘗試創建示例應用程序 – Lixas

回答

0

我已經找到了一個工作輪到此。由於我的圖拒絕在更新後致電figure.savefig(),所以我找到了一個方法來處理它。我的數字是HBox2容器內(GUI與格萊德3.6.7創建)作爲第一要素

# some stuff going 
    figure.saveFig(fileName) 
# WORK-A-ROUND: delete figure after calling savefig() 
    box = self.win.get_widget('hbox2') 
    box.remove(box.get_children()[0]) 
    self._figPrepare() 

def _figPrepare(self): #initialize graph 
    figure = Figure() 
    canvas = FigureCanvasGTK(figure) # a gtk.DrawingArea 
    canvas.show()  
    figure.clf() 
    gui.w().set("figure", figure) 
    self.win.get_widget('hbox2').pack_start(canvas, True, True) # this will be aded to last place 
    self.win.get_widget('hbox2').reorder_child(canvas, 0) #place plot to space where it should be 

我知道這是不是最好的做法,而且很可能是緩慢的,但它爲我的工作確定。希望別人會從http://matplotlib.org/examples/user_interfaces/embedding_in_gtk2.html

什麼似乎有助於發現這很有

1

您是否試過更新行數據而不是重新創建圖形?這假設數據點的數量不會改變每一幀。它可能會幫助拒絕更新的問題,至少它會更快。

def _updateGraph(self, fig, x, x1, y): 
    #Various calculations done here 


    ydata = numpy.array(y)/(1.0**1) 

    # retrieved the saved line object 
    line = getattr(fig, 'animated_line', None); 

    if line is None: 
     # no line object so create the subplot and axis and all 
     fig.clf() 
     axis = fig.add_subplot(111) 

     axis.grid(True) 
     #remove ticks and labels 
     axis.get_xaxis().set_ticks_position("none") 
     for i in range(len(axis.get_xticklabels())): 
      axis.get_xticklabels()[i].set_visible(False) 
     axis.get_yaxis().set_ticks_position("none")    
     xdata = numpy.array(x); 
     line = axis.plot(xdata, ydata, "k-" ,alpha=.2) 
     axis.set_title('myTitle') 
     fig.autofmt_xdate() 

     # save the line for later reuse 
     fig.animated_line = line 
    else: 
     line.set_ydata(ydata) 
    fig.canvas.draw() 
0

是「AGG」不知道這意味着什麼,但固定的這個錯誤對我來說:)

from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas