简体   繁体   English

savefig调用后的Python matplotlib更新图

[英]Python matplotlib Update figure after savefig called

My problem is: I have Matplotlib figure in PyGTK application, that is constatly updated each few seconds. 我的问题是:我在PyGTK应用程序中有Matplotlib图,该图每隔几秒钟会不断更新。 I've added abbility to save figure to disk as PNG file. 我添加了将图形保存为PNG文件到磁盘的功能。 After calling figure.savefig(filename, other parameters) my figure in application stops being updated. 调用figure.savefig(filename, other parameters)我的应用程序中的图形停止更新。

Figure initialization phase: 图初始化阶段:

# 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

Figure is being updated this way (this called each few seconds in separate thread): 正在以这种方式更新图形(在单独的线程中每隔几秒钟调用一次):

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

everything works as expected. 一切都按预期进行。 But after calling: 但在致电后:

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

File have been saved, BUT my figure on screen stops being updated . 文件已保存,但屏幕上的图形停止更新

Any ideas how do I save figure to disk and still be able to update my fig on screen ? 有什么想法可以将图形保存到磁盘上并且仍然能够在屏幕上更新图形吗?

Have you tried updating the line data instead of recreating the figure? 您是否尝试过更新线数据而不是重新创建图形? This assumes the number of datapoints doesn't change each frame. 假设每个帧的数据点数量均不变。 It might help issue of things refusing to update, and at the least it will be faster. 这可能有助于解决拒绝更新的问题,至少它会更快。

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

I have found a work-a-round to this. 我已经找到了解决方案。 As my figure refuses to be updated after calling figure.savefig() so i found a way how to work a round it. 由于我的图在调用figure.savefig()后拒绝更新,因此我找到了一种方法来对其进行处理。 My figure is within HBox2 container (GUI is created with Glade 3.6.7) as first element 我的图形位于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

I know this is not best practice, and probably is slow, but it work OK for me. 我知道这不是最佳做法,并且可能很慢,但是对我来说效果不错。 Hope someone else will find this useful 希望其他人会发现这个有用

from http://matplotlib.org/examples/user_interfaces/embedding_in_gtk2.html 来自http://matplotlib.org/examples/user_interfaces/embedding_in_gtk2.html

what seems to help is the "agg" not sure what that means but fixed this bug for me :) 似乎有帮助的是“ agg”,不确定该是什么意思,但为我修复了此错误:)

from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM