为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕桂英4014372 2022-10-25 14:49:44
我正在尝试重新创建此视频中的动画。目前,我的代码绘图,但每一帧都与最后一帧相同。我试图首先绘制顶点,然后一次绘制每个点。这些点是预先计算的,所以我想要的只是绘制当前点和它之前的点。下一步将是我制作动画(编辑:即 gif 或 mp4),但那是在我可以让这部分工作之后。import matplotlib.pyplot as pltfrom matplotlib.animation import FuncAnimationimport numpy as npimport randomvertices = np.array([[0,0],            [2,0],            [1,2.5]])dots = 100def 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 pointspoints = 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 indexindex = 0def 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()
查看完整描述

1 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

您似乎误解了matplotlib.animation.funcanimation的工作原理,我强烈建议您查看网上可以找到的众多示例中的一些。让我们试试这个版本,如下所示:

import matplotlib.pyplot as plt

import matplotlib.animation as animation

import numpy as np

import random


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

                     [2,0],

                     [1,2.5],

                     [0,0]])


dots = 1000


# lower and higher bounds for x to be generated

int_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 points

points = 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 boundaries

ax.plot(vertices[:,0],vertices[:,1],'k-', linewidth=1)

# initialize scatter object for current step and all evaluated dots

scat_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_dots


def 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_dots


anim = 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

如果您有任何问题,我会添加更多评论,但由于这主要是您自己在这里的工作,我相信您会发现您所尝试的远比应该做的复杂:)。希望这可以帮助。



查看完整回答
反对 回复 2022-10-25
  • 1 回答
  • 0 关注
  • 139 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号