Python 创建随时间变化的图形视频

Python 创建随时间变化的图形视频,python,animation,Python,Animation,我想创建一个视频的图形,因为它随着时间的推移演变。我曾尝试将图形的PNG图像拼接在一起,但它有10000帧,这需要很长时间。我现在想尝试使用animate.FuncAnimation(),但我遇到了很多麻烦。以下是我到目前为止的情况: def plot(fname, haveMLPY=False): # Load data from .npz file. data = np.load(fname) X = data["X"] T = data["T"] N

我想创建一个视频的图形,因为它随着时间的推移演变。我曾尝试将图形的PNG图像拼接在一起,但它有10000帧,这需要很长时间。我现在想尝试使用
animate.FuncAnimation()
,但我遇到了很多麻烦。以下是我到目前为止的情况:

def plot(fname, haveMLPY=False):
    # Load data from .npz file.
    data = np.load(fname)
    X = data["X"]
    T = data["T"]
    N = X.shape[1]
    A = data["vipWeights"]
    degrees = A.sum(1)
    ksB = data["ksB"]

    # Initialize a figure.
    figure = plt.figure()


    files=[] 
    # filename for the name of the resulting movie
    filename = 'animation'
    from mpl_toolkits.mplot3d import Axes3D  
    for i in range(10**4):
         mp = X[i,:,0]
        data2 = np.c_[degrees, ksB, mp]

        # Create best fit surface for data2
        # regular grid covering the domain of the data
        mn = np.min(data2, axis=0)
        mx = np.max(data2, axis=0)
        X_grid, Y_grid = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20))
        XX = X_grid.flatten()
        YY = Y_grid.flatten()
        order = 2    # 1: linear, 2: quadratic
        if order == 1:
            # best-fit linear plane
            A = np.c_[data2[:,0], data2[:,1], np.ones(data2.shape[0])]
            C,_,_,_ = scipy.linalg.lstsq(A, data2[:,2])    # coefficients

            # evaluate it on grid
            Z = C[0]*X_grid + C[1]*Y_grid + C[2]

            # or expressed using matrix/vector product
            #Z = np.dot(np.c_[XX, YY, np.ones(XX.shape)], C).reshape(X.shape)

        elif order == 2:
            # best-fit quadratic curve
            A = np.c_[np.ones(data2.shape[0]), data2[:,:2], np.prod(data2[:,:2], axis=1), data2[:,:2]**2]
            C,_,_,_ = scipy.linalg.lstsq(A, data2[:,2])

            # evaluate it on a grid
            Z = np.dot(np.c_[np.ones(XX.shape), XX, YY, XX*YY, XX**2, YY**2], C).reshape(X_grid.shape)

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.plot_surface(X_grid, Y_grid, Z, rstride=1, cstride=1, alpha=0.2)
        ax.scatter(degrees, ksB, mp)
        ax.set_xlabel('degrees')
        ax.set_ylabel('ksB')
        ax.set_zlabel('mp')
        # form a filename
        fname2 = '_tmp%03d.png'%i
        # save the frame
        savefig(fname2)
        # append the filename to the list
        files.append(fname2)
    # call mencoder 
    os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o " + filename + ".mpg")
    # cleanup
    for fname2 in files: os.remove(fname2)
所有代码都来自

# Create best fit surface for data2

通常可以忽略,因为它仅用于计算数据的最佳拟合平面

基本上,有N个神经元,每个神经元都有三个重要的属性,我想画出来:度、ksB和mp。只有mp随时间变化。mp的所有数据都存储在X中。格式X[i,i,i]表示X[时间,神经元,数据类型]。现在,我正在循环X[I,:,0](mp是第0个变量)。拍摄所有10^4张图片的截图需要花费很长时间,mp的坐标轴也在不断变化

有没有办法加快速度(使用
animation.FuncAnimation
或其他方法)并防止轴移动每个帧

谢谢

fig = plt.figure()