title: Can someone tell me what's wrong with my animation code?
body: <h1>Can someone tell me what's wrong with my animation code?</h1>
  
  <p><p>I coded this program that will generate 40 png images, and an animated gif image.</p><p>The animation is supposed to be a sphere that is sliced into 5 segments, and the segments move left and right, but, as you will be able to see, it doesn't really work as I planned</p><p>(I only posted 12 frames of the gif, as 40 frames will be to big to post)</p> <p>Can someone tell me how to correct it?</p></p>
  
  <pre><code>import matplotlib.pyplot as plt
  from numpy import sin,cos,pi,outer,ones,size,linspace
  from mpl_toolkits.mplot3d import axes3d
  
  # Define the x, y, and z lists for the sphere:
  x = 10*outer(cos(linspace(0, 2*pi)), sin(linspace(0, pi)))
  y = 10*outer(sin(linspace(0, 2*pi)), sin(linspace(0, pi)))
  z = 10*outer(ones(size(linspace(0, 2*pi))), cos(linspace(0, pi)))
  
  for n in range(40):
      fig = plt.figure(figsize=(10, 10))
      ax = fig.add_subplot(111, projection='3d')
      ax.plot_surface(x, y, z, color = ('r'))
      ax.set_xticks([])
      ax.set_yticks([])
      ax.set_zticks([])
      ax.set_xlim(-10,10)
      ax.set_ylim(-10,10)
      ax.set_zlim(-10,10)
      plt.savefig(str(n)+'.png') #Save the image into a numbered png file
      print(str(n)+'.png completed.')
      plt.close()
  
      sign = 1
      for count in range(5): #Slice the sphere into 5 segments
          for num in range(len(z)//5*count,len(z)//5*(count+1)):
              z[num] += sign # Make the segments go positive and negative directions
              sign *= -1
  
  from PIL import Image
  images = []
  # Open each png file and store in images list
  for n in range(40):
      exec('a'+str(n)+'=Image.open("'+str(n)+'.png")')
      images.append(eval('a'+str(n)))
  # Create an animated gif file:
  images[0].save('ball.gif',
                 save_all=True,
                 append_images=images[1:],
                 duration=100,
                 loop=0)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/JIAxK.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JIAxK.gif" alt="Part of the resulting animation"></a></p>