更新python动画时删除先前的散点图

更新python动画时删除先前的散点图,python,animation,matplotlib,scatter-plot,Python,Animation,Matplotlib,Scatter Plot,尽管数小时的浏览确实改善了我的python动画代码,但我还是不太明白一件事,因此我转向社区中善良的灵魂,希望有人能给我一些启发 简言之,我有一个大约2000 x 1000像素的背景图像,比如说,我需要在这个图像上散布一些点,并对过程进行动画处理,然后将整个过程保存为视频。我可以根据需要更新散点图,但不能删除之前的散点图。所以输出不是我真正想要的。如果有人能看一下代码,看看哪里出了问题,我会很高兴的。我使用了scat.remove(),它似乎什么都没做 提前谢谢各位 import matplotl

尽管数小时的浏览确实改善了我的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()

在代码中,您已经在循环完成之前删除了最后一个散点;因此,将添加一些散点图,然后立即删除。
可以通过收集列表中的散点,然后使用
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个代表点。不过,只有一句话:最终mp4我在导入powerpoint幻灯片时遇到了一些问题。看起来它缺少64位编解码器。有什么想法吗?没有。我不知道如何在powerpoint中插入mp4。这将是一个完全不同的问题,我想,甚至在Stackoverflow这个话题上都不会。这救了我一天。非常感谢你!