【问题标题】:Animating one image on top of another in matplotlib在 matplotlib 中将一个图像放在另一个图像之上
【发布时间】:2019-12-13 21:04:43
【问题描述】:

我一直在使用 matplotlib 为一些图像制作动画,但我现在发现我想为这些动画添加更多信息,所以我想覆盖一个散点图来指示重要特征。到目前为止,这是我用来生成电影的代码:

def make_animation(frames,path,name): 

    plt.rcParams['animation.ffmpeg_path'] = u'/Users/~/anaconda3/bin/ffmpeg' #ffmpeg path    
    n_images=frames.shape[2] 
    assert (n_images>1)   
    figsize=(10,10)
    fig, ax = plt.subplots(figsize=figsize)
    fig.tight_layout()
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
    #lineR, = ax.plot(xaxis_data[0],R_data[0],'c-',label="resources")
    img = ax.imshow(frames[:,:,0], animated = True)   


    def updatefig(img_num): 

        #lineR.set_data(xaxis_data[img_num],R_data[img_num],'r-')

        img.set_data(frames[:,:,img_num])

        return [img]


    ani = animation.FuncAnimation(fig, updatefig, np.arange(1, n_images), interval=50, blit=True)
    mywriter = animation.FFMpegWriter(fps = 20)
    #ani.save('mymovie.mp4',writer=mywriter)

    ani.save("/Users/~/output/"+ path + "/" + name + ".mp4",writer=mywriter)

    plt.close(fig)

我想在每一帧的顶部添加一个散点图,就像我可以使用这样的常规图:

fig, ax = plt.subplots()
img = ax.imshow(frames[:,:,0])
img = ax.scatter(scatter_pts[0],scatter_pts[1],marker='+',c='r')

我的第一次尝试是这样的:

def make_animation_scatter(frames,path,name,scatter): 

    plt.rcParams['animation.ffmpeg_path'] = u'/Users/~/anaconda3/bin/ffmpeg' #ffmpeg path    
    n_images=frames.shape[2] 
    assert (n_images>1)   
    figsize=(10,10)
    fig, ax = plt.subplots(figsize=figsize)
    fig.tight_layout()
    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)
    #lineR, = ax.plot(xaxis_data[0],R_data[0],'c-',label="resources")
    img = ax.imshow(frames[:,:,0], animated = True)   
    img = ax.scatter(scatter[0],scatter[1],c='r',marker = '+')

    def updatefig(img_num): 

        #lineR.set_data(xaxis_data[img_num],R_data[img_num],'r-')

        img.set_data(frames[:,:,img_num])
        img = ax.scatter(scatter[0],scatter[1],c='r',marker = '+')
        return [img]


    ani = animation.FuncAnimation(fig, updatefig, np.arange(1, n_images), interval=50, blit=True)
    mywriter = animation.FFMpegWriter(fps = 20)
    #ani.save('mymovie.mp4',writer=mywriter)

    ani.save("/Users/~/output/"+ path + "/" + name + ".mp4",writer=mywriter)

    plt.close(fig)

这会产生一个没有散点图的视频,所以我想知道如何正确实现它。

【问题讨论】:

  • “它不起作用”到底是什么意思?
  • 它生成的视频只有帧而不是散点图。我已将问题编辑得更清楚,感谢您的提问。
  • 下面的答案能解决这个问题吗? (如果有,请采纳)
  • 是的,我之前没有时间测试它。

标签: python matplotlib animation


【解决方案1】:

docs 表示,当使用blit=True 时,您必须从更新函数返回“艺术家的可迭代”才能重绘它们。但是,您只返回img。此外,您正在用图像和分散对象覆盖img。相反,您想要的是为散点图使用不同的名称,例如

img = ax.imshow(frames[:,:,0], animated = True)
sct = ax.scatter(scatter[0],scatter[1],c='r',marker = '+')

两者仍将绘制在同一轴上,但现在您有 imgsct 艺术家,然后更新功能将是

def updatefig(img_num, img, sct, ax):
    img.set_data(frames[:,:,img_num])
    sct = ax.scatter(scatter[0], scatter[1], c='r', marker='+')
    return [img, sct]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2012-11-17
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多