[英]Extracting 2D Matrix from 3D Matrix for Schur Decompostion in Python
[英]Python: Issue with animation from 3D matrix
我試過運行以下代碼:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm #color map
u = np.array([[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,2,0],[0,0,0]],[[0,0,0],[0,3,0],[0,0,0]]])
x = [-1,0,1]
y = [-1,0,1]
X,Y = np.meshgrid(x,y)
fig = plt.figure()
ax = axes3d.Axes3D(fig)
def update(i, ax, fig):
ax.cla()
wframe = ax.plot_surface(X, Y, u[:,:,i],rstride=1,cstride=1,cmap=cm.coolwarm)
ax.set_zlim3d(-1,1)
print("step= ",i)
return wframe
ani = animation.FuncAnimation(fig,update,frames=3,fargs=(ax, fig),interval=1000)
ani.save('ani.mp4',bitrate=-1,codec="libx264")
然而,動畫不能正常運行 - 它需要2倍的素數步驟(i = 0),然后,它再次循環。 你能幫我解決這個問題嗎?
添加了init
函數, repeat=False
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm #color map
u = np.array([[[0,0,0],[0,1,0],[0,0,0]],[[0,0,0],[0,2,0],[0,0,0]],[[0,0,0],[0,3,0],[0,0,0]]])
x = [-1,0,1]
y = [-1,0,1]
X,Y = np.meshgrid(x,y)
fig = plt.figure()
ax = axes3d.Axes3D(fig)
def init():
wframe = ax.plot_surface([], [], [], rstride=1, cstride=1, cmap=cm.coolwarm)
return wframe
def update(i, ax, fig):
ax.cla()
wframe = ax.plot_surface(X, Y, u[:,:,i],rstride=1,cstride=1,cmap=cm.coolwarm)
ax.set_zlim3d(-1,1)
print("step= ",i)
return wframe
ani = animation.FuncAnimation(fig,update,init_func=init, frames=3,fargs=(ax, fig),interval=1000,repeat=False)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.