Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Animate wireframe in matplotlib using IDLE

Hi all,

I'm trying to figure out how to animate a wireframe (3d plot) using FuncAnimation. I can understand the animated 2d examples and can create a wireframe plot, but I have trouble updating and plotting the new at each step.

I am trying to make an exercise for a Python introduction course for physicists. The example below should do the basics, i.e. define a mesh, initialise it to 0 everywhere and increase the level at each iteration step. If this basic first step would work I would be ok ... I hope.

Thanks for any help,


from matplotlib import pyplot as plt
from pylab import *
from mpl_toolkits.mplot3d.axes3d import Axes3D  #-- voor 3d assen
from matplotlib import animation
import time

def test_wave():

  #-- initialize grid
  Xvalues = linspace(0, 1, 10)
  Yvalues = linspace(0, 1, 10)
  X,Y = meshgrid(Xvalues, Yvalues)
  Z = X+Y

  # -----------------------------
  def init_wave_profile(): 
  # -----------------------------
      for i in range (0,10):
        for j in range (0,10):
          Z[i][j] = 0

  # ----------------------------------------------
  def update_wave_profile(itime,Z,p1):
  # ----------------------------------------------
      print itime
      for i in range (0,10):
        for j in range (0,10):
          Z[i][j] = itime

  #-- define plot and initialize profile (flat)
  fig = plt.figure(figsize=(6,6))
  ax = fig.add_subplot(1, 1, 1, projection='3d')
  p1 = ax.plot_wireframe(X, Y, Z, rstride=1, cstride=1, color = 'blue')
  ax.set_ylim3d(-0.1, 1.1);
  ax.set_zlim3d(-0.5, 1.5);

  #-- animate the profile   
  anim = animation.FuncAnimation(
            fig         = fig,
            func       = update_wave_profile,
            init_func = init_wave_profile,
            frames   = 10,
            interval  = 200,
            blit        = False,
            fargs     = (Z, p1)