随着时间的推移创建图形视频

时间:2016-03-14 17:39:22

标签: python animation

我希望随着时间的推移创建一个图形视频。我已经尝试将图形的PNG图像拼接在一起,但它有10,000帧,这需要很长时间。我现在想尝试使用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

fig = plt.figure()

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

基本上,有N个神经元,每个神经元都有三个我想要绘制的重要属性:degrees,ksB和mp。只有mp随时间变化。 mp的所有数据都存储在X中。格式X [i,i,i]表示X [时间,神经元,数据类型]。现在,我循环遍历X [i,:,0](mp是第0个变量)。截取所有10 ^ 4图像的截图需要永久,而mp的轴不断变化。

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

谢谢!

0 个答案:

没有答案