所以我试图使用以下代码在python中制作动画:
import numpy as np
import matplotlib.pyplot as plt
from scipy import linalg
from scipy.linalg import toeplitz
from matplotlib import cm
from matplotlib import animation
from mpl_toolkits.mplot3d import Axes3D
#----------------------------------------------
#This Section of code defines all of the important constants
timetot=1500*3
timestep=2
tdel=int(timetot/timestep)
rodleng=0.5
sizestep=0.01
sdel=int(rodleng/sizestep)
thermdiff=59/(450*7900)
matcon=timestep*thermdiff/(sizestep**2)
#----------------------------------------------
#stuff for ploting
X, T = np.meshgrid(np.arange(0, sdel), np.arange(0, tdel))
colorinterpolation = 50
colourMap = plt.cm.jet
#------------------------------------------------
#Seting up initial state and Toeplitz matrix
mat=np.zeros(sdel)
np.put(mat, [0, 1], [1+2*matcon, -matcon])
mattoe=toeplitz(mat)
mattoe[0,0]=1
mattoe[0,1]=0
mattoe[-1,-1]=1
mattoe[-1,-2]=0
lu, piv=linalg.lu_factor(mattoe)
Rod=np.empty(sdel)
Rod.fill(20)
Rod[0]=1000
Rod[-1]=0
XTmat=np.ones((tdel,sdel))
XTmat[0, :]=Rod
#print(mattoe)
#-----------------------------------------
for i in range(1, tdel):
delt=linalg.lu_solve((lu, piv), XTmat[i-1, :])
XTmat[i, :]=delt
figa = plt.figure()
axa = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = axa.plot([], [], lw=2)
def init():
line.set_data([], [])
return line,
def animate(i):
dw=XTmat[i, :]
x=np.linspace(0, 50)
y=dw
line.set_data(x, y)
return line,
anim = animation.FuncAnimation(figa, animate, init_func=init,
frames=1000, interval=20, blit=True)
plt.show()
我很确定我的错误是在animate函数中,因为创建XTmat的其余代码工作得很好,其余的动画代码都从这里翻录http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
答案 0 :(得分:0)
显然,您的数据在x轴上的范围是0到50,在y轴上的范围是0到1000。但是,您将视图限制在xlim=(0, 2), ylim=(-2, 2)
范围内。此范围内没有数据,因此视图保持为空。
设置范围以适应您的数据以查看数据
axa = plt.axes(xlim=(0, 50), ylim=(-2, 1000))