## matplotlib-users

 [Matplotlib-users] surface plot From: David Craig - 2012-02-23 16:14:15 ```Hi, I have an array defined by 3 variables p(x,z,t). I would like to produce a surface plot with colors defined by p and animate it. That is plot the value of p at all x and z, over time (t). My code to get p is below but I really have no idea how to plot this. Anyone know the best way to go about this? thanks, D # 2D Finite Distance Wave Equation. from pylab import * from numpy import math ion() # Set up variables. nx = 100 nz = 100 nsteps = 300 c = 3500 dt = 10**-4 h = 1 t = arange(0,nsteps,dt) # Define source as a spike. s = zeros(nsteps) s[1] = 1 s[2] = 2 s[3] = 1 # Position source. xs = 50 zs = 50 ##plot(t,s) ##show() # Set up pressure field. p=empty([nx,nz,nsteps]) for t in range(0,nsteps-1): for z in range(0,nz-1): for x in range(0,nx-1): p[x,z,t] = 0 # Solve wave equation. for t in range(2,nsteps-1): for z in range(1,nz-1): for x in range(2,nx-1): p[xs,zs,t] = s[t] k = (c*dt/h)**2 p[x,z,t] = 2*p[x,z,t-1] - p[x,z,t-2] + k*(p[x+1,z,t-1]-4*p[x,z,t-1]+p[x-1,z,t-1]+p[x,z+1,t-1]+p[x,z-1,t-1]) #Plot somehow draw() #close() ```
 Re: [Matplotlib-users] surface plot From: Benjamin Root - 2012-02-23 16:23:54 Attachments: Message as HTML ```On Thu, Feb 23, 2012 at 10:14 AM, David Craig wrote: > Hi, > I have an array defined by 3 variables p(x,z,t). I would like to produce > a surface plot with colors defined by p and animate it. That is plot the > value of p at all x and z, over time (t). My code to get p is below but > I really have no idea how to plot this. Anyone know the best way to go > about this? > thanks, > D > > > # 2D Finite Distance Wave Equation. > from pylab import * > from numpy import math > > ion() > > # Set up variables. > nx = 100 > nz = 100 > nsteps = 300 > c = 3500 > dt = 10**-4 > h = 1 > t = arange(0,nsteps,dt) > > # Define source as a spike. > s = zeros(nsteps) > s[1] = 1 > s[2] = 2 > s[3] = 1 > > # Position source. > xs = 50 > zs = 50 > ##plot(t,s) > ##show() > > > # Set up pressure field. > p=empty([nx,nz,nsteps]) > > for t in range(0,nsteps-1): > > for z in range(0,nz-1): > > for x in range(0,nx-1): > > p[x,z,t] = 0 > > > > # Solve wave equation. > for t in range(2,nsteps-1): > > > for z in range(1,nz-1): > > for x in range(2,nx-1): > > p[xs,zs,t] = s[t] > > k = (c*dt/h)**2 > > p[x,z,t] = 2*p[x,z,t-1] - p[x,z,t-2] + > k*(p[x+1,z,t-1]-4*p[x,z,t-1]+p[x-1,z,t-1]+p[x,z+1,t-1]+p[x,z-1,t-1]) > > > #Plot somehow > draw() > #close() > > > Here is an example using wireframe(). This method of animation is not recommended anymore, but it does still work. Once you get your plot_surface version working, there are some various tricks to speed it up and make it more efficient. Probably the most important thing to remember is that your norm object will need to be set before starting so that the colors are consistent throughout the animation. http://matplotlib.sourceforge.net/examples/mplot3d/wire3d_animation_demo.html Cheers! Ben Root ```