title: Matplotlib animation.FuncAnimation: Custom frame generator only yields once
body: <p>I'm encountering a strange problem with the matplotlib animation. I'm trying to create a animated bar plot using the following code:</p>
  
  <pre><code>import os, time
  from PIL import Image, ImageSequence
  import numpy as np
  import matplotlib.pyplot as plt
  import matplotlib.patches as patches
  import matplotlib.path as path
  import matplotlib.animation as animation
  
  import blackxample
  
  FILE_PREFIX = "cell-isotohyper"
  FILE_SUFFIX = ".tif"
  FILE_PATH   = "./example-video"
  XCUT = (91, 91+266)
  YCUT = (646, 646+252)
  
  LIMIT = 100
  OFFSET = 0
  
  Y_SCALE = 3000
  NUM_OF_BINS = 37
  
  BAR_WIDTH = 1.0
  BAR_COLOR = 'b'
  RANGE = range(0, NUM_OF_BINS//2+1)
  
  def animate(i, fig, ax, bars):
      # = np.random.randn(1000)
      print(len(i))
      for a in RANGE:
          bars[a].set_height(i[a])
      return (fig, ax, bars)
  
  def main():
      fig, ax = plt.subplots()
      ax.set_ylim(0, Y_SCALE)
      ax.set_xlim(0, NUM_OF_BINS//2+1)
  
      bars = ax.bar(np.arange(NUM_OF_BINS), [i for i in range(NUM_OF_BINS)], BAR_WIDTH, color=BAR_COLOR)
  
      ani = animation.FuncAnimation(fig, animate, xframes, fargs = (fig, ax, bars), interval=500)
      plt.show()
  </code></pre>
  
  <p>This code snippet works completely fine if I'm using randomly generated data or constant via:</p>
  
  <pre><code>def xframes():
      i = 0
      while i &lt; 100:
          yield [2312.7094266223335, 27.238786592368257, 75.252063484372513, 13.678304922077643, 11.879804374653929, 21.900570139020687, 2.930771773796323, 11.945594479736741, 10.88517941461987, 4.4176609254771506, 4.1075871395528338, 1.248363771876285, 1.4798157379442216, 3.5285036346353564, 3.2583080973651732, 3.4640042567344267, 3.130503535456981, 0.67334205875304676, 0.71393606581800562]
          #yield np.histogram(np.random.randn(1000), NUM_OF_BINS//2 + 1)[0]
          i+=1
  </code></pre>
  
  <p>Using the function, aframes, instead, does only yield the first item if it is used together animation.FuncAnimation(). If aframe is iterated manually, however, <strong>the generator works completely fine</strong>.</p>
  
  <pre><code>def aframes():
      list_of_files = []
      for dirname, dirnames, filenames in os.walk(FILE_PATH):
          for filename in filenames:
              if filename.startswith(FILE_PREFIX) and filename.endswith(FILE_SUFFIX):
                  list_of_files.append(os.path.join(FILE_PATH, filename))
  
      # Open every picture - in every file
      count = 0
      imagecount = 0
      framecount = 0
      skipped = 0
      for file in list_of_files:
          framecount = 0
          a = Image.open(file)
          for frame in ImageSequence.Iterator(a):
              if count &gt; OFFSET and count &lt;= OFFSET+LIMIT:
                  # Cut image beforehand - probably faster
                  frame = frame.crop((XCUT[0], YCUT[0], XCUT[1], YCUT[1]))
                  # Load image intro Matrix
                  imageMatrix = blackxample.Matrix.fromPillow(frame)
                  try:
                      imageMatrix.findContour()
                      imageMatrix.calculateCentroid()
                      imageMatrix.transform(NUM_OF_BINS)
                      #yield imageMatrix.getTransform()
                      yield [2312.7094266223335, 27.238786592368257, 75.252063484372513, 13.678304922077643, 11.879804374653929, 21.900570139020687, 2.930771773796323, 11.945594479736741, 10.88517941461987, 4.4176609254771506, 4.1075871395528338, 1.248363771876285, 1.4798157379442216, 3.5285036346353564, 3.2583080973651732, 3.4640042567344267, 3.130503535456981, 0.67334205875304676, 0.71393606581800562]
                  except blackxample.NoConvergenceError:
                      skipped+=1
              print("[", count ,"] done")
  
              framecount+=1
              count+=1
          imagecount+=1
  
  # Test for frame iterator - works fine
  #for i in _frames():
  #    print(i)
  </code></pre>
  
  <p><strong>Does someone has a clue what and why is happening? How can I fix it?</strong></p>
  
  <p>The generator also runs as expected if the three imageMatrix-lines inside the try-block are commented out which suggests that there is an error inside imageMatrix.findContour(). But what am I looking for? findContour doesn't do anything weird</p>