【发布时间】:2018-03-17 00:10:27
【问题描述】:
所以我正在尝试使用以下代码在 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/
【问题讨论】:
-
究竟是什么是问题?问题需要清晰的问题描述。
-
我得到了一个坐标系的静止图像,没有发生任何事情。当我尝试链接中的代码时,一切正常,因此设置不是问题。对迟到的回复表示歉意。
标签: python-3.x animation matplotlib