3D使用Matplotlib 3D波动画

时间:2014-05-05 20:03:55

标签: python matplotlib 3d

我是一名法国学生,我目前正在研究波浪,我正在试图制作波浪的3D情节,但我在python中不够好。可以使用plt.show()绘制第一个曲面,但如果在循环中我放了plt.draw(),我没有图形输出。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

if 'qt' in plt.get_backend().lower():
    try:
        from PyQt4 import QtGui
    except ImportError:
        from PySide import QtGui

fig = plt.figure()

# PARAMETRES PHYSIQUES
C = 0.05  #VIT ONDE 
Lx = 6.0  #taille du domaine
Ly = 6.0  #taille du domaine
T = 5.0   #temps d'integration

# PARAMETRES NUMERIQUES
NX = 151  #nombre de points de grille
NY = 101  #nombre de points de grille
NT = 250  #nombre de pas de temps

dx = Lx/(NX-2) #pas de grille (espace)
dy = Ly/(NY-2) #pas de grille (espace)
dt = T/NT      #pas de grille (temps)


alpha=(C**2*dt**2/dx**2)


# Pour la figure
xx = np.zeros((NX,NY))
yy = np.zeros((NX,NY))
for i in np.arange(0,NX):
   for j in np.arange(0,NY):
      xx[i,j]=i*dx
      yy[i,j]=j*dy

plt.ion()
#ax = plt.gca(projection='3d')
ax = fig.add_subplot(111, projection='3d')

a=40.0

#Initialisation
ddU   = np.zeros((NX,NY))
U_data = np.exp(-a*((xx-(float(Lx)/2))**2+(yy-(float(Lx)/2))**2))
U_old= U_data.copy()
U_new = np.zeros((NX,NY))

surf = ax.plot_surface(xx,yy,U_data,vmin=-0.1,vmax=0.1,cmap=cm.jet,antialiased=False,linewidth=0,rstride=1,cstride=1)

plt.show()

# Boucle en temps
for n in np.arange(0,NT):
   ddU[1:-1,1:-1] = (U_data[2:,1:-1]-2*U_data[1:-1,1:-1]+U_data[:-2,1:-1])/(dx**2) \
                  + (U_data[1:-1,2:]-2*U_data[1:-1,1:-1]+U_data[1:-1,:-2])/(dy**2)

   U_new[1:-1,1:-1]=2*U_data[1:-1,1:-1]-U_old[1:-1,1:-1] + alpha*ddU[1:-1,1:-1]

   toto=U_old
   U_old=U_data
   U_data=U_new
   U_new=toto

# FIXED BC
#   U_data[0,:]=0.0

# REFLECTING BC
   U_data[0,:]=U_data[2,:]
   U_data[NX-1,:]=U_data[NX-3,:]
   U_data[:,0]=U_data[:,2]
   U_data[:,NY-1]=U_data[:,NY-3]

# PERIODIC BC
#   U_data[0,:]=U_data[NX-2,:]
#   U_data[NX-1,:]=U_data[1,:]
#   U_data[:,0]=U_data[:,NY-2]
#   U_data[:,NY-1]=U_data[:,1]

   if (n%10==0):
     plotlabel= "N = " + str(n+1)

     ax.cla()
     surf = ax.plot_surface(xx,yy,U_data,vmin=-0.1,vmax=0.1,cmap=cm.jet,antialiased=False,linewidth=0,rstride=1,cstride=1)
     ax.set_zlim3d(-0.1,0.1)

     plt.draw()

     if 'qt' in plt.get_backend().lower():
        QtGui.qApp.processEvents()

plt.show()

我需要一张传播波的3D图,比如[http://www.youtube.com/watch?v=bYOeA6r8o-I]

0 个答案:

没有答案