【问题标题】:Remove precedent scatterplot while updating python animation更新python动画时删除先例散点图
【发布时间】:2017-08-21 20:07:11
【问题描述】:

虽然数小时的 stackoverflow 浏览确实改进了我的 python 动画代码,但我无法完全弄清楚一件事,因此我转向社区的善良灵魂,希望有人能够解开一些谜团。

简而言之,我有一个大约 2000 x 1000 像素的背景图像,比方说,我需要在该图像上散布一些点并对过程进行动画处理并将整个图像保存为视频。我只能根据需要更新散点图,但我无法删除先前的散点图。所以输出不是我真正想要的。如果有人可以看一下代码并查看故障所在,我会很高兴。我用过 scat.remove() 似乎什么也没做。

提前谢谢各位。

import matplotlib.pyplot as plt
import pylab as pl
import numpy as np
from pylab import savefig
from matplotlib import animation
import matplotlib



######################################################
fig = plt.figure()
ax = plt.axes()
a = plt.imread('background.jpg')    
im = plt.imshow(a)

#######################################################

def randpair(n):
    x,y=[],[]
    for i in xrange(n):
        x.append(np.random.randint(100,1900))
        y.append(np.random.randint(100,900))
    return x,y


def animate(i):

    scat = ax.scatter(0,0,color='white')
    points = np.random.randint(5,size=10)

    for j in points: 
        xy = randpair(j)
        x = xy[0]
        y = xy[1]
        print x,y

        if len(x) > 0 :
            scat.remove()
            scat = ax.scatter(x,y,color='r',s=18)


        plt.xticks([])
        plt.yticks([])



    return scat,ax,  # ax returns the text to be updated and scat returns the scatterplot.


anim = animation.FuncAnimation(fig, animate, 49,interval=300, blit=True)
writer = animation.writers['ffmpeg']
anim.save('film_3.mp4')
#plt.show()

【问题讨论】:

    标签: python animation matplotlib scatter-plot


    【解决方案1】:

    在代码中,您已经在循环完成之前删除了最后一个散点;所以会添加一些散点图,然后立即删除。
    可以通过收集列表中的散点图来防止这种情况,然后使用remove 从画布中删除列表中的散点图并清理列表。

    除此之外,返回完整的ax 对象有点没用。所以我建议简单地关闭 blitting,因为保存动画并不重要。

    这是对我有用的完整代码:

    import matplotlib.pyplot as plt
    import numpy as np
    import matplotlib.animation
    
    fig = plt.figure()
    ax = plt.axes()
    scats = []
    a = np.random.rand(8,18)
    im = ax.imshow(a, cmap="YlGn", vmin=0, vmax=3, extent=[0,2000,0,1000])
    plt.xticks([])
    plt.yticks([])
    
    def randpair(n):
        x,y=[],[]
        for i in xrange(n):
            x.append(np.random.randint(100,1900))
            y.append(np.random.randint(100,900))
        return x,y
    
    def animate(i):
        global scats
        # first remove all old scatters
        for scat in scats:
            scat.remove()
        scats=[]
        # now draw new scatters
        points = np.random.randint(5,size=10)
        for j in points: 
            x, y = randpair(j)
            if len(x) > 0 :
                scats.append(ax.scatter(x,y,color='r',s=18))  
    
    anim = matplotlib.animation.FuncAnimation(fig, animate, 50,
                                    interval=1000, blit=False)
    
    writer = matplotlib.animation.FFMpegWriter(fps=15, 
                codec="h264", 
                extra_args=["-preset", "veryslow","-crf","0"])
    anim.save(__file__+".mp4", writer=writer)
    
    plt.show()
    

    【讨论】:

    • 非常感谢@ImportanceOfBeingEarnest。您根据散点列表处理问题的想法为我节省了一天的时间。
    • 我刚刚接受了您的回答,很抱歉没有早点这样做,因为我只是不知道如何(我是该网站的新手)。有人投了赞成票,但我还没有超过 15 个代表点。
    • 只是一句话:Final mp4 我在导入PowerPoint幻灯片时遇到了一些麻烦。看起来它缺少 64 位编解码器。有什么想法吗?
    • 没有。我对将 mp4 插入 powerpoint 一无所知。我猜这将是一个完全不同的问题,甚至不是 Stackoverflow 上的主题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 2019-12-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    相关资源
    最近更新 更多