保存matplotlib动画会导致错误

时间:2017-07-27 16:51:28

标签: python animation matplotlib ffmpeg

我创建了一个程序(见下文),它从pandas数据帧中获取位置,力和时间。目标是根据与之相关的时间数据绘制位置与力并绘制动画。

到目前为止,动画运行良好,但无法保存动画,无论是作为gif还是mp4。我已经在线阅读了无数的解决方案,但这些问题似乎都无法解决。

我使用的是OS X El Capitan版本10.11.6和python3.6。我使用了brew install ffmpegbrew install mencoder。对于我都得到错误

 File "animation.py", line 45, in <module>
    ani.save('test.mp4', writer=writer)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/animation.py", line 1009, in save
    for a in all_anim]):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/animation.py", line 1009, in <listcomp>
    for a in all_anim]):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/animation.py", line 1482, in new_saved_frame_seq
    return itertools.islice(self.new_frame_seq(), self.save_count)
ValueError: Stop argument for islice() must be None or an integer: 0 <= x <= sys.maxsize.

我用pip来安装我的很多其他软件包,比如pandas,所以这可能是问题的根源?

以下是我的代码。

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas

TABLE = pandas.read_csv("Data.csv")


fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

xs=[]
ys=[]

def animate(interval):

    time = interval

    #convert to TIME series to int for handling purposes
    TABLE.TIME = TABLE.TIME.astype(int)

    if time in TABLE.TIME.unique(): 

        POSIT_series = TABLE[TABLE.TIME == time].POSIT
        POSIT_list = POSIT_series.tolist()
        x = POSIT_list[0]

        FORCE_series = TABLE[TABLE.TIME == time].FORCE
        FORCE_list = FORCE_series.tolist()
        y = FORCE_list[0]

        xs.append(x)
        ys.append(y)

        ax1.clear()
        ax1.plot(xs,ys)
    return

FRAMES= TABLE.TIME.astype(int).max()    
plt.rcParams['animation.ffmpeg_path']='/usr/local/bin/ffmpeg'
writer = animation.FFMpegWriter()
ani = animation.FuncAnimation(fig, animate, interval=1, frames=FRAMES, repeat=False)
plt.show()

#same error for writer = 'mencoder' and writer ='ffpmeg
ani.save('test.mp4', writer=writer)

我正在练习的数据。最终,我将在更大的数据文件上运行此代码。

TESTNUM,POINTNUM,TIME,POSIT,FORCE,EXT,CH5,CH6,CH7,CH8
1550,1,0.9,0.00055,10.59274006,-0.00382513,,,,
1550,2,1.049,0.0006,10.80716419,0.0001464,,,,
1550,3,1.34,0.00085,12.23668289,-2.749E-05,,,,
1550,4,1.54,0.001,14.26660252,-7.324E-05,,,,
1550,5,1.938,0.001275,15.7961874,0.0001464,,,,
1550,6,3,0.016550001,19.67018318,0.00181191,,,,
1550,7,4,0.016625,30.94909668,0.00188507,,,,
1550,8,5,0.0167,25.63127708,0.00183932,,,,
1550,9,6,0.016799999,18.42650795,0.00197664,,,,
1550,10,7,0.016925,16.52525139,0.00201322,,,,
1550,11,8,0.017774999,18.09771156,0.00226945,,,,
1550,12,9,0.018300001,19.49864578,0.00216879,,,,
1550,13,10,0.019099999,21.24265671,0.0022237,,,,
1550,14,11,0.019424999,22.77224159,0.0022237,,,,
1550,15,12,0.0197,24.18746758,0.00225112,,,,
1550,16,13,0.019974999,26.23167801,0.00224203,,,,

1 个答案:

答案 0 :(得分:0)

这是一个已知错误 - 请参阅此处http://plnkr.co/edit/SPO4ZMhfNQFm7du8w1xT?p=preview。从本质上讲,numpy int不是python int的子类,在这种情况下最终会导致问题。

作为一种解决方法,我通过添加修改了animation.py代码 self.save_count = int(self.save_count)在此函数的开头(见下文)。这会将self.save_count的类型从'numpy.int64'更改为'int',现在动画可以正确保存。我将在matplotlib上将此标记为问题。

def new_saved_frame_seq(self):
    # Generate an iterator for the sequence of saved data. If there are
    # no saved frames, generate a new frame sequence and take the first
    # save_count entries in it.
    self.save_count = int(self.save_count)
    print(type(self.save_count))

    if self._save_seq:
        # While iterating we are going to update _save_seq
        # so make a copy to safely iterate over
        self._old_saved_seq = list(self._save_seq)
        return iter(self._old_saved_seq)
    else:
        return itertools.islice(self.new_frame_seq(), self.save_count)