簡體   English   中英

savefig調用后的Python matplotlib更新圖

[英]Python matplotlib Update figure after savefig called

我的問題是:我在PyGTK應用程序中有Matplotlib圖,該圖每隔幾秒鍾會不斷更新。 我添加了將圖形保存為PNG文件到磁盤的功能。 調用figure.savefig(filename, other parameters)我的應用程序中的圖形停止更新。

圖初始化階段:

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

文件已保存,但屏幕上的圖形停止更新

有什么想法可以將圖形保存到磁盤上並且仍然能夠在屏幕上更新圖形嗎?

您是否嘗試過更新線數據而不是重新創建圖形? 假設每個幀的數據點數量均不變。 這可能有助於解決拒絕更新的問題,至少它會更快。

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

我已經找到了解決方案。 由於我的圖在調用figure.savefig()后拒絕更新,因此我找到了一種方法來對其進行處理。 我的圖形位於HBox2容器(GUI用Glade 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

似乎有幫助的是“ agg”,不確定該是什么意思,但為我修復了此錯誤:)

from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM