gpt4 book ai didi

python - 保存 matplotlib 动画会导致错误

转载 作者:行者123 更新时间:2023-12-04 23:26:22 27 4
gpt4 key购买 nike

我创建了一个程序(见下文),它从 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,,,,

最佳答案

这是一个已知的错误 - 请参阅此处 http://bugs.python.org/issue30537 .本质上,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)

关于python - 保存 matplotlib 动画会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45356718/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com