title: python - Update matplotlib animation in Tkinter FigureCanvasTkAgg with Button/ clear looping animation from canvas
body: <p>I'm trying to create a GUI with tkinter that shows me an animation of an event I captured and when I click on a Button the next event should be shown. So far my code does exactly that but the problem I have is that when I click on the button to display the next event, the old event is not cleared from the canvas and thus an overlap of the two, or when I click more often numerous animations is shown. </p>
  
  <p>My code looks something like that:</p>
  
  <pre><code>import matplotlib.pyplot as plt
  from matplotlib import animation
  import Tkinter as tk
  from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  
  # Framenumber of Images that shall be animated
  # shape(Frames) = (number_of_events, frames_per_event)
  Frames = get_Frames(tdms_file)
  
  # event that shall be analysed
  k = 0
  
  fig = plt.figure()
  ax = fig.add_subplot(111)
  
  root = tk.Tk()
  root.title("My Animation GUI")
  
  canvas = FigureCanvasTkAgg(fig, master=root)
  canvas.show()
  canvas.get_tk_widget().grid(row=0,column=1)
  
  def event_animation(Frames, ax, k, canvas):
  
      ims = []
  
      for i in range(len(Frames[k])):
  
          Image = get_image(Frames[k], i)     # function that grabs the images from a different file 
          im =  ax.imshow(Image, cmap='gray', animated=True)
          ims.append([im])
  
      ani = animation.ArtistAnimation(fig, ims, interval=25, blit=True, repeat_delay=1000)
      canvas.show()
  
  # show next event
  def next_event():
      global k
      k += 1
      ani_show = event_animation(Frames, ax, k, canvas)
  
  # show previous event
  def prev_event():
      global k
      k -= 1
      ani_show = event_animation(Frames, ax, k, canvas)
  
  prev_cell_button = tk.Button(root, text='&lt;--', width=10, command = prev_cell)
  prev_cell_button.grid(row=0, column=0)
  next_cell_button = tk.Button(root, text='--&gt;', width=10, command = next_cell)
  next_cell_button.grid(row=0, column=2)
  
  tk.mainloop()
  </code></pre>
  
  <p>I already tried clearing the axes with <code>ax.cla()</code> before creating a new animation and edited my animation function like in this example:
  <a href="https://stackoverflow.com/questions/16732379/stop-start-pause-in-python-matplotlib-animation">stop / start / pause in python matplotlib animation</a></p>
  
  <p>The function to create the animation will then look like this: </p>
  
  <pre><code>def event_Funcanimation(Frames, ax, k, canvas):
  
      Image = get_image(Frames[k], 0)
      im = ax.imshow(Image, cmap='gray', animated=True)
  
      def ani_iterator():
          i = 0
          while i &lt; len(Frames[k]):
              if not stop:
  
                  Image = get_image(Frames[k], i)
                  i += 1
                  yield Image
  
  
      def show_image(ani_iterator):
          Image = ani_iterator
          im.set_array(Image)
          return im,
  
      ani = animation.FuncAnimation(fig, show_image, ani_iterator, 
                                    blit=True, interval=25, repeat = True)
      canvas.draw()
      return ani
  </code></pre>
  
  <p>where the <code>stop</code> variable in the while loop will be changed when I click on one of the buttons. I know that this won't clear the animation from the axes but only stop it, but it doesn't work anyway for my code.</p>
  
  <p>The problem right now is, that the animation keeps looping in the canvas and when a new animation is started they overlap. I think this issue could be solved pretty easily if there is a possibility clear the animation from the canvas before starting a new one. But I couldn't find any solution that helps me with my problem. And I want the animation loop, so setting <code>repeat = False</code> is not an option. Also this doesn't solve the issue, as the animations will still overlap, unless they are finished.</p>
  
  <p>Thanks in advance for any help.</p>