17

I'm plotting some data in a Tkinter FigureCanvasTkagg using matplotlib. I need to clear the figure where I plot data and draw new data when a button is pressed.

Here is the plotting part of the code (there's an App class defined before):

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

How do I update the contents of such a canvas?

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Copo
  • 181
  • 1
  • 2
  • 6
  • Are you embedding this canvas into some GUI app, or are you just using the pyplot/pylab functionality? – BrenBarn Aug 25 '12 at 18:00

1 Answers1

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

hope this helps

Oblivion
  • 1,669
  • 14
  • 14
Oblivion
  • 171
  • 2
  • For some reason, after many draw() operations, I get a max recursion depth reached error.. Which never happens without that draw call... – Mr.WorshipMe Oct 30 '18 at 18:32