使用matplotlib保存散点图动画会生成空白的视频文件 [英] Saving scatterplot animations with matplotlib produces blank video file

查看:272
本文介绍了使用matplotlib保存散点图动画会生成空白的视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a very similar problem to this question

,但建议的解决方案对我来说不起作用。

but the suggested solution doesn't work for me.

我已经使用matplotlib动画模块设置了动画散点图。当它显示现场时,这可以正常工作。我想保存到一个avi文件或类似的东西。我写的代码是不会出错的,但它生成的视频只显示一组空白的轴或黑色的屏幕。我已经做了几个检查,数据正在运行,数据更新只是没有被保存到视频...

I have set up an animated scatter plot using the matplotlib animation module. This works fine when it is displaying live. I would like to save it to an avi file or something similar. The code I have written to do this does not error out but the video it produces just shows a blank set of axes or a black screen. I've done several checks and the data is being run and figure updated it's just not getting saved to video...

我尝试删除animated = True和 blit = True,如此问题所建议的,但这并没有解决问题。

I tried removing "animated=True" and "blit=True" as suggested in this question but that did not fix the problem.

我已经将相关代码放在下面,但如果需要,可以提供更多的代码。有没有人会建议我应该做什么来使这个工作?

I have placed the relevant code below but can provide more if necessary. Could anyone suggest what I should do to get this working?

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)


推荐答案

延迟。我已经找到了一个工作,只需保存大量的单个图像,然后调用ffmpeg链接在一起。这不是理想的,但完成工作。 (较大类的一部分)

Sorry for the delay. I have found a work around by simply saving lots of individual images and then calling ffmpeg to chain them together. This isn't ideal but gets the job done. (part of larger class)

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"

这篇关于使用matplotlib保存散点图动画会生成空白的视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆