title: Matplotlib FuncAnimation Step-by-Step Animation Function
body: <p>I am trying to use <code>matplotlib</code>'s <code>FuncAnimation</code> to make an animated video. Each frame is just a boolean <code>n</code> x <code>n</code> array visualised as white/black squares. I can do this successfully by defining all the arrays in advance and then going through them one by one. This uses code similar to <a href="https://matplotlib.org/stable/gallery/animation/simple_anim.html" rel="nofollow noreferrer"><code>matplotlib</code>'s example</a>.</p>
  <p>My items are rather large and I want to run the simulation for a long time. I thus don't want to create the entire list of arrays then go through them one by one. Instead, I want to define the <code>animate</code> function to do each step. Let me explain with a minimal non-working example. My actual example includes far larger arrays!</p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  from matplotlib.animation import FuncAnimation
  
  def create_video(n):
      global X
      X = np.random.binomial(1, 0.3, size = (n,n))
      
      fig = plt.figure()
      im = plt.imshow(X, cmap = plt.cm.gray)
      
      def animate(t):
          global X
          X = np.roll(X, +1, axis = 0)
          im.set_array(X)
      
      anim = FuncAnimation(
          fig,
          animate,
          frames = 100,
          interval = 1000 / 30,
          blit = True
      )
      
      return anim
  
  anim = create_video(10)
  </code></pre>
  <p>This initialises some random <code>10</code> x <code>10</code> set of <code>0</code>/<code>1</code>s then just 'rolls' it at each step. I get an error.</p>
  <blockquote>
  <p>RuntimeError: The animation function must return a sequence of Artist objects.</p>
  </blockquote>
  <p>If I remove the <code>return anim</code>, replacing it with <code>pass</code>, and replacing <code>anim = create_video(10)</code> with <code>create_video(10)</code>, then I get a warning.</p>
  <blockquote>
  <p>UserWarning: Animation was deleted without rendering anything. This is most likely unintended. To prevent deletion, assign the Animation to a variable that exists for as long as you need the Animation.</p>
  </blockquote>
  <p>Clearly, I don't understand well enough <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.FuncAnimation.html" rel="nofollow noreferrer"><code>FuncAnimation</code></a>. What I want to happen is for the function <code>animate</code> to update the array <code>X</code>, by 'rolling' it one step, as well as doing <code>im.set_array(X)</code>.</p>