保存散点图动画与matplotlib产生空白视频文件(Saving scatterplot anim

2019-08-17 16:08发布

我有一个非常类似的问题到这个问题

但建议的解决方案并没有为我工作。

我已经建立了使用matplotlib动画模块的动画散点图。 当它显示实时这工作得很好。 我想将它保存到一个AVI文件或类似的东西。 我写这样做并不会报错了代码,但它只是产生的视频显示一个空白组轴或黑屏。 我已经做了几项检查,并在数据正在运行图更新,它只是没有得到保存的视频...

我试图在建议删除“动画= true”和“位图传送=真” 这个问题,但没有解决问题。

我已经把相关的代码以下,但如果需要的话可以提供更多。 可能有人建议我应该怎么做来得到这个工作?

def initAnimation(self):
        rs, cfgs = next(self.jumpingDataStreamIterator)     
        #self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], marker='o')
        self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], marker='o', animated=True)
        return self.scat,


def updateAnimation(self, i):
    """Update the scatter plot."""
    rs, cfgs = next(self.jumpingDataStreamIterator)
    # Set x and y data...
    self.scat.set_offsets(rs[:2,].transpose()) 
    #self.scat = self.axAnimation.scatter(rs[0], rs[1], c=cfgs[0], animated=True)
    # Set sizes...
    #self.scat._sizes = 300 * abs(data[2])**1.5 + 100
    # Set colors..
    #self.scat.set_array(cfgs[0])
    # We need to return the updated artist for FuncAnimation to draw..
    # Note that it expects a sequence of artists, thus the trailing comma.
    matplotlib.pyplot.draw()
    return self.scat,

def animate2d(self, steps=None, showEvery=50, size = 25):
    self.figAnimation, self.axAnimation = matplotlib.pyplot.subplots()
    self.axAnimation.set_aspect("equal")
    self.axAnimation.axis([-size, size, -size, size])
    self.jumpingDataStreamIterator = self.jumpingDataStream(showEvery)

    self.univeseAnimation = matplotlib.animation.FuncAnimation(self.figAnimation, 
                            self.updateAnimation, init_func=self.initAnimation,
                            blit=True)
    matplotlib.pyplot.show()

def animate2dVideo(self,fileName=None, steps=10000, showEvery=50, size=25):
    self.figAnimation, self.axAnimation = matplotlib.pyplot.subplots()
    self.axAnimation.set_aspect("equal")
    self.axAnimation.axis([-size, size, -size, size])
    self.Writer = matplotlib.animation.writers['ffmpeg']
    self.writer = self.Writer(fps=1, metadata=dict(artist='Universe Simulation'))
    self.jumpingDataStreamIterator = self.jumpingDataStream(showEvery)

    self.universeAnimation = matplotlib.animation.FuncAnimation(self.figAnimation, 
                            self.updateAnimation, scipy.arange(1, 25), init_func=self.initAnimation)

    self.universeAnimation.save('C:/universeAnimation.mp4', writer = self.writer)

Answer 1:

抱歉耽搁了。 我已经简单地省下很多单独的图像,然后调用的ffmpeg到链在一起发现周围的工作。 这是不理想,但能够完成任务。 (较大类的一部分)

def easyway(self, frames):
    ##INSERT CODE TO GET xdata and ydata!
    for i in range(0, frames):
       fileName = "videoName"+"%04d.png" % i
       matplotlib.pyplot.scatter(xdata,ydata,c=coldata, marker='*',
                              s=15.0, edgecolor='none')
       matplotlib.pyplot.savefig(self.workingDirectory+'temp/'+fileName, dpi=dpi)
       matplotlib.pyplot.close()   
       ##INSERT CODE TO UPDATE xdata and ydata!
    self.createVideoFile(fps)#calls FFMpeg to chain together all the pictures

    if cleanUp:#removes all the picture files created in the process
        print "temporary picture files being removed..."
        self.clearDirectory()
    print "FINISHED"

def clearDirectory(self,directoryName='temp'):
    files = glob.glob(self.workingDirectory+directoryName+"/*")
    for f in files:
        os.remove(f)

def createVideoFile(self,fps=3):
    command="ffmpeg -f image2 -r %s -i %s%s" % (str(fps), self.workingDirectory+'temp/', self.universe.description)
    command+="%04d.png"
    command+=" -c:v libx264 -r 30 %s.mp4" % (self.workingDirectory+'videos/'+self.universe.description)
    print "Running command:"
    print command
    p = subprocess.Popen(command, shell=True, stdout = subprocess.PIPE, stderr=subprocess.STDOUT)
    output = p.communicate()[0]
    print "output\n"+"*"*10+"\n"
    print output
    print "*"*10
    print "Video file has been written"


文章来源: Saving scatterplot animations with matplotlib produces blank video file