在 Gif 中保存 matplotlib 动画时出错

Error saving matplotlib animation in Gif

我在将一组二维数组保存到 Gif 中时遇到问题。我搜索了类似的标题,但没有找到相同的问题。

我有600个带数据的文件,都是93*226的float数组。我只需要将它们绘制成一个 gif 文件。

import numpy as np
import os
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib
from  matplotlib.animation import FuncAnimation

# reading files
path = '/home/Anton/Heat'
files = os.listdir(path)
files.sort()
print('Number of files:', len(files))
arr = []

for i in range(len(files[0:150])):  # read files
    file = files[i]
    df = pd.read_csv(path + '/'+ file, sep = ' ', skiprows = 2, header = None) # skip 2 lines
    arr.append(df.values[:,1:]) # skip 1st col
arr = np.array(arr)

fig = plt.figure(figsize=(14,25))
zmin, zmax = 50, 1150  # scale for Z values
norm = matplotlib.colors.Normalize(vmin=zmin, vmax=zmax, clip=False)
im = plt.imshow(np.transpose(arr[0,:,:]), animated = True, cmap = plt.cm.gist_heat, norm=norm)
plt.grid(False)
nst = arr.shape[0] # number of frames in Gif == number of files read

def update(i):
    im.set_array(np.transpose(arr[i,:,:]))
    return im

# create and save animation
anim = FuncAnimation(fig, update, frames=range(nst), interval=50, repeat = False)
anim.save('/home/Anton/Heat_1/Heating_1.gif', writer='imagemagick')

所以如果我在循环中设置要读取的文件数 = 50

for i in range(len(files[0:50]))

,正常工作。或者,如果我读取所有 600 个文件但以小分辨率保存它们

fig = plt.figure(figsize=(10,7))
...
anim.save('/home/Anton/Heat_1/Heating_1.gif', dpi = 50, writer='imagemagick')

它也有效。

但是 当我设置的文件数大于 ~50(例如 150,如上所示)and/or 更大的 figsize,我得到一个错误:

ValueError                                Traceback (most recent call last)
<ipython-input-171-7897cf1b47b2> in <module>()
     91 anim = FuncAnimation(fig, update, interval=50, repeat = False)
---> 92 anim.save('/home/zizin/Heat_1/Heating_1.gif', writer='imagemagick')

~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
   1258                         # TODO: See if turning off blit is really necessary
   1259                         anim._draw_next_frame(d, blit=False)
-> 1260                     writer.grab_frame(**savefig_kwargs)
~/anaconda3/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
     97                 value = type()
     98             try:
---> 99                 self.gen.throw(type, value, traceback)
~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in saving(self, fig, outfile, dpi, *args, **kwargs)
    235             yield self
    236         finally:
--> 237             self.finish()
~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in finish(self)
    367     def finish(self):
    368         '''Finish any processing for writing the movie.'''
--> 369         self.cleanup()
~/anaconda3/lib/python3.6/site-packages/matplotlib/animation.py in cleanup(self)
    406     def cleanup(self):
    407         '''Clean-up and collect the process used to write the movie file.'''
--> 408         out, err = self._proc.communicate()
~/anaconda3/lib/python3.6/subprocess.py in communicate(self, input, timeout)
    841 
    842             try:
--> 843                 stdout, stderr = self._communicate(input, endtime, timeout)
~/anaconda3/lib/python3.6/subprocess.py in _communicate(self, input, endtime, orig_timeout)
   1503                     selector.register(self.stdin, selectors.EVENT_WRITE)
   1504                 if self.stdout:
-> 1505                     selector.register(self.stdout, selectors.EVENT_READ)
~/anaconda3/lib/python3.6/selectors.py in register(self, fileobj, events, data)
    349 
    350         def register(self, fileobj, events, data=None):
--> 351             key = super().register(fileobj, events, data)
~/anaconda3/lib/python3.6/selectors.py in register(self, fileobj, events, data)
    235             raise ValueError("Invalid events: {!r}".format(events))
    236 
--> 237         key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
~/anaconda3/lib/python3.6/selectors.py in _fileobj_lookup(self, fileobj)
    222         """
    223         try:
--> 224             return _fileobj_to_fd(fileobj)
~/anaconda3/lib/python3.6/selectors.py in _fileobj_to_fd(fileobj)
     37         except (AttributeError, TypeError, ValueError):
     38             raise ValueError("Invalid file object: "
---> 39                              "{!r}".format(fileobj)) from None
ValueError: Invalid file object: <_io.BufferedReader name=54>

我已经删除了作者='imagemagick' 来自

anim.save('/home/Anton/Heat_1/Heating.gif', writer='imagemagick')

现在可以了。 ¯\_(ツ)_/¯