14

我正在使用 Tkinter FigureCanvasTkagg 绘制一些数据matplotlib。当按下按钮时,我需要清除绘制数据并绘制新数据的图形。

这是代码的绘图部分(之前定义了一个 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()   

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

4

1 回答 1

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

希望这可以帮助

于 2012-08-29T22:52:51.523 回答