智慧大石
您似乎误解了matplotlib.animation.funcanimation的工作原理,我强烈建议您查看网上可以找到的众多示例中的一些。让我们试试这个版本,如下所示:import matplotlib.pyplot as pltimport matplotlib.animation as animationimport numpy as npimport randomvertices = np.array([[0,0], [2,0], [1,2.5], [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): 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])# evaluating all your pointspoints = np.array([[0.25, 0.1]])for j in range(dots-1): 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(): scat_curr.set_offsets(np.c_[vertices[0,0], vertices[0,1]]) scat_dots.set_offsets(np.c_[vertices[0,0], vertices[0,1]]) return scat_curr, scat_dotsdef animate(i): scat_curr.set_offsets(np.c_[points[i,0], points[i,1]]) scat_dots.set_offsets(np.c_[points[:i,0], points[:i,1]]) ax.legend([scat_curr],['iter i = {a}'.format(a=i)], loc='upper left') 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 如果您有任何问题,我会添加更多评论,但由于这主要是您自己在这里的工作,我相信您会发现您所尝试的远比应该做的复杂:)。希望这可以帮助。