编程语言
首页 > 编程语言> > python – 如何更新FigureCanvasTkAgg的内容

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,python,tkinter,tkinter-canvas
来源: https://codeday.me/bug/20191004/1854464.html