如何更新FigureCanvasTkAgg的内容

时间:2012-08-25 17:57:10

标签: python tkinter updating tkinter-canvas

我正在使用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()   

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

1 个答案:

答案 0 :(得分:10)

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

希望这会有所帮助