python – 如何更新FigureCanvasTkAgg的内容

我正在使用matplotlib在Tkinter FigureCanvasTkagg中绘制一些数据.
我需要清除图中我绘制数据的位置,并在按下按钮时绘制新数据.

这是代码的绘图部分(之前定义了一个App类):

    self.fig = figure()
    self.ax = self.fig.add_subplot(111)
    self.ax.set_ylim( min(y), max(y) )      

    self.line, = self.ax.semilogx(x, y, '.-')   #tuple of a single element
    self.canvas = FigureCanvasTkAgg(self.fig, master=master)
    self.ax.semilogx(x, y, 'o-')
    self.canvas.show()
    self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
    self.frame.pack()   

如何更新此类画布的内容?

解决方法:

#call the clear method on your axes
self.ax.clear()

#plot the new data
self.ax.set_ylim(min(newy), max(newy))
self.ax.semilogx(newx, newy, 'o-')

#call the draw method on your canvas
self.canvas.draw()

希望这可以帮助

上一篇:(Updating)【区间操作数据结构例题】


下一篇:「动态 DP」学习笔记 (updating...)