python - 如何使用 matplotlib/Mayavi 创建由 2 个空间坐标定义的表面的 3D 动画?

标签 python animation matplotlib mayavi

这是用于离散化有限差分格式的一维扩散方程的数字代码。每个时间步长都会获得速度,我想为该解决方案制作动画,以便可视化扩散下速度相对于时间的演变。任何帮助,将不胜感激。谢谢!

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D ##library for 3d projection plots
from matplotlib import cm ##cm = "colormap" for changing the 3d plot color palette

###variable declarations
nx = 31
ny = 31
nt = 17
nu=.05
dx = 2.0/(nx-1)
dy = 2.0/(ny-1)
sigma = .25
dt = sigma*dx*dy/nu

x = np.linspace(0,2,nx)
y = np.linspace(0,2,ny)

u = np.ones((ny,nx)) ##create a 1xn vector of 1's
un = np.ones((ny,nx)) ##

###Assign initial conditions

u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2 ##set hat function I.C. : u(.5<=x<=1 && .5<=y<=1 ) is 2

fig = plt.figure()
ax = fig.gca(projection='3d')
X,Y = np.meshgrid(x,y)
surf = ax.plot_surface(X,Y,u[:], rstride=1, cstride=1, cmap=cm.coolwarm,
    linewidth=0, antialiased=False)
plt.show()
ax.set_xlim(0,2)
ax.set_ylim(0,2)
ax.set_zlim(1,2.5)
#ax.zaxis.set_major_locator(LinearLocator(5))

###Run through nt timesteps

u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2

for n in range(nt+1): 
    un[:] = u[:]
    u[1:-1,1:-1]=un[1:-1,1:-1]+nu*dt/dx**2*(un[2:,1:-1]-2*un[1:-1,1:-1]+un[0:-2,1:-1])+nu*dt/dy**2*   (un[1:-1,2:]-2*un[1:-1,1:-1]+un[1:-1,0:-2])    

    u[0,:]=1
    u[-1,:]=1

    u[:,0]=1
    u[:,-1]=1

fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X,Y,u[:], rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=True)
ax.set_zlim(1,2.5)
plt.show()

最佳答案

这是使用 Mayavi 的一种方法 - 阅读评论以获取一些解释:

import numpy as np
import time

# import mayavi's mlab API for scripting
from mayavi import mlab

###variable declarations
nx = 31
ny = 31
nt = 17
nu=.05
dx = 2.0/(nx-1)
dy = 2.0/(ny-1)
sigma = .25
dt = sigma*dx*dy/nu

x = np.linspace(0,2,nx)
y = np.linspace(0,2,ny)

u = np.ones((ny,nx)) ##create a 1xn vector of 1's
un = np.ones((ny,nx)) ##

###Assign initial conditions

u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2 ##set hat function I.C. : u(.5<=x<=1 && .5<=y<=1 ) is 2
X,Y = np.meshgrid(x,y)

###Run through nt timesteps

u[.5/dy:1/dy+1,.5/dx:1/dx+1]=2

# create a surface from grid-shaped data
surf = mlab.mesh(X,Y,u[:])

t = time.time()
max_framerate = 10
for n in range(nt+1): 
    un[:] = u[:]
    u[1:-1,1:-1]=un[1:-1,1:-1]+nu*dt/dx**2*(un[2:,1:-1]-2*un[1:-1,1:-1]+un[0:-2,1:-1])+nu*dt/dy**2*   (un[1:-1,2:]-2*un[1:-1,1:-1]+un[1:-1,0:-2])    

    u[0,:]=1
    u[-1,:]=1

    u[:,0]=1
    u[:,-1]=1

    # the mlab_source attribute of surf represents the data we're plotting.
    # it has x, y and z attributes as you'd expect. here we only need to
    # update the z attribute
    surf.mlab_source.z = u

    # there's no need to call any equivalent to matplotlib's draw() or show() 
    # functions - another draw event gets triggered automatically whenever
    # surf's data source gets modified

    # put a pause in here to control the maximum framerate
    while time.time() - t < (1./max_framerate):
        pass
    t = time.time()

您可以在 Mayavi 中找到有关动画数据的更多信息 here .

关于python - 如何使用 matplotlib/Mayavi 创建由 2 个空间坐标定义的表面的 3D 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21504849/

相关文章:

python - 在 Python 中打印时处理 NoneType 对象的好方法

python - PyQT5 标签的鼠标悬停事件

python - x 轴上的 Matplotlib 日期时间

python - 智能峰值检测方法

python - Keras LSTM 神经网络 : TypeError: LSTM() missing 1 required positional argument: 'Y'

python - 你如何计算一系列随机点的面积?

jquery - 如何为全屏页面制作流畅的css动画?

c# - .NET 动画窗口

ios - 如何在按钮单击 ios Swift 上执行具有不同点的相同动画

python - 如何在 cartopy/matplotlib 图上显示公里标尺?