1

I have a wxPython GUI in which I use matplotlib for 2D and 3D graphics.

I was having problems getting a surface plot to animate, so I used the following dummy case adapted from someplace online to test. It is a fairly typical example for 3D animation and works fine when run standalone.

if True:
  from mpl_toolkits.mplot3d import axes3d
  import numpy as np
  import matplotlib.pyplot as plt
  import matplotlib.animation as animation
  def generate(X, Y, phi):
    R = 1 - np.sqrt(X**2 + Y**2)
    return np.cos(2 * np.pi * X + phi) * R

  fig = plt.figure()
  ax = axes3d.Axes3D(fig)

  xs = np.linspace(-1, 1, 3)
  ys = np.linspace(-1, 1, 3)
  X, Y = np.meshgrid(xs, ys)
  Z = generate(X, Y, 0.0)
  wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)
  ax.set_zlim(-1,1)

  def update(i, ax, fig):
    print i
    ax.cla()
    phi = i * 360 / 2 / np.pi / 100
    Z = generate(X, Y, phi)
    wframe = ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2)
    ax.set_zlim(-1,1)
    return wframe

  ani = animation.FuncAnimation(fig, update, 
      frames=xrange(100), 
      fargs=(ax, fig), interval=100)
plt.show()

The "True" test is of course unnecessary but was meant to replicate something like the structure in the GUI at the point of execution (to check for any scoping issues).

When I insert the exact same code into my GUI with a wx.Button causing execution of the code, it plots only the first frame and nothing else, but doesn't issue any error either (running inside IDLE). I can verify by printing that exactly one (the first iteration i=0) frame is plotting.

This is exactly the behavior also of the actual data of interest which originated the problem.

Thank you.

4
  • Are you keeping a reference to ani inside the GUI? If you've put this code inside a method, the reference would be garbage collected if you didn't put it into a member variable or something of the like. Commented Mar 26, 2015 at 17:41
  • @Ajean That was the ticket. I simply changed ani to self.ani and it works. Thanks. But, do you know why this would happen with certain animations and not others? When I used ArtistAnimation with the same data in a list of images in the same place in the GUI without self.ani it works. (plots an image that changes) Commented Mar 27, 2015 at 1:35
  • I'm not sure ... they both inherit from TimedAnimation I think, but for the nitty gritty I suspect you'd have to dive into the code itself. Commented Mar 27, 2015 at 15:17
  • Also, posted the comment as an answer since it solved the problem. Commented Mar 27, 2015 at 15:30

1 Answer 1

1

When using an animation inside of a GUI class, make sure to keep the reference to the animation object in a class member so that it doesn't get garbage collected at the end of the method in which it is created. Using something like

self.ani = animation.FuncAnimation(....

rather than

ani = animation.FuncAnimation(....

should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.