Matplotlib 动画为每一帧绘制相同的东西

我正在尝试重新创建视频中的动画。

目前,我的代码绘图,但每一帧都与最后一帧相同。我试图首先绘制顶点,然后一次绘制每个点。这些点是预先计算的,所以我想要的只是绘制当前点和它之前的点。下一步将是我制作动画(编辑:即 gif 或 mp4),但那是在我可以让这部分工作之后。

import matplotlib.pyplot as plt

from matplotlib.animation import FuncAnimation

import numpy as np

import random


vertices = np.array([[0,0],

            [2,0],

            [1,2.5]])


dots = 100


def newPos(index, old_x, old_y):

    vertex_x = vertices[index][0]

    vertex_y = vertices[index][1]

    new_x = 0.5*(vertex_x + old_x)

    new_y = 0.5*(vertex_y + old_y)

    return([new_x,new_y])


global points

points = np.array([[0.25, 0.1]])


for i in range(dots-1):

    points = np.concatenate((points, [newPos(random.randint(0,2), points[i][0], points[i][1])]), axis = 0)


plt.figure()


global index

index = 0


def animate(i):

    plt.cla()


    global index

    index += 1


    plt.plot(vertices[0][0], vertices[0][1], 'o')


    global points

    plt.plot(points[0:index][0], points[0:index][1], 'o', color = '#1f77b4')


    plt.legend(['index = {0}'.format(index)], loc='upper left')


    plt.tight_layout()


while index < dots:

    ani = FuncAnimation(plt.gcf(), animate, interval=15000/dots)


    plt.title('Chaos Game with {0} Vertices and {1} Steps'.format(len(vertices), dots))


    plt.show()


慕桂英4014372
浏览 140回答 1
1回答

智慧大石

您似乎误解了matplotlib.animation.funcanimation的工作原理,我强烈建议您查看网上可以找到的众多示例中的一些。让我们试试这个版本,如下所示:import matplotlib.pyplot as pltimport matplotlib.animation as animationimport numpy as npimport randomvertices = np.array([[0,0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[2,0],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[1,2.5],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[0,0]])dots = 1000# lower and higher bounds for x to be generatedint_lb = np.min(vertices[:,0])int_hb = np.max(vertices[:,0])def newPos(index, old_x, old_y):&nbsp; &nbsp; vertex_x = vertices[index][0]&nbsp; &nbsp; vertex_y = vertices[index][1]&nbsp; &nbsp; new_x = 0.5*(vertex_x + old_x)&nbsp; &nbsp; new_y = 0.5*(vertex_y + old_y)&nbsp; &nbsp; return([new_x,new_y])# evaluating all your pointspoints = np.array([[0.25, 0.1]])for j in range(dots-1):&nbsp; &nbsp; points = np.concatenate((points, [newPos(random.randint(int_lb,int_hb), points[j][0], points[j][1])]), axis=0)fig = plt.figure()ax = plt.gca()ax.set_xlim([np.min(vertices[:,0])-0.05*np.max(vertices[:,0]),1.05*np.max(vertices[:,0])])ax.set_ylim([np.min(vertices[:,1])-0.05*np.max(vertices[:,1]),1.05*np.max(vertices[:,1])])ax.set_title('Chaos Game with {a} Vertices and {b} Steps'.format(a=len(vertices)-1, b=dots))# draw boundariesax.plot(vertices[:,0],vertices[:,1],'k-', linewidth=1)# initialize scatter object for current step and all evaluated dotsscat_curr = ax.scatter([], [], marker='X', s=15, c='black')scat_dots = ax.scatter([], [], marker='o', s=5, c='#1f77b4',zorder=-1)def init():&nbsp; &nbsp; scat_curr.set_offsets(np.c_[vertices[0,0], vertices[0,1]])&nbsp; &nbsp; scat_dots.set_offsets(np.c_[vertices[0,0], vertices[0,1]])&nbsp; &nbsp; return scat_curr, scat_dotsdef animate(i):&nbsp; &nbsp; scat_curr.set_offsets(np.c_[points[i,0], points[i,1]])&nbsp;&nbsp;&nbsp; &nbsp; scat_dots.set_offsets(np.c_[points[:i,0], points[:i,1]])&nbsp;&nbsp;&nbsp; &nbsp; ax.legend([scat_curr],['iter i = {a}'.format(a=i)], loc='upper left')&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return scat_curr, scat_dotsanim = animation.FuncAnimation(fig, animate, init_func=init, frames=dots, interval=10)Writer = animation.writers['ffmpeg']writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)anim.save('some_nice_triforces.mp4', writer=writer)这使:https://i.stack.imgur.com/6X68l.gif 如果您有任何问题,我会添加更多评论,但由于这主要是您自己在这里的工作,我相信您会发现您所尝试的远比应该做的复杂:)。希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python