【发布时间】:2020-05-23 00:01:50
【问题描述】:
我使用plt.plot_surface() 和plt.scatter() 创建了一系列3D 图像,如下所示:
我想将它们保存为 .gif。在this example 之后,我能够循环查看视角并收集图像:
v_angles = [item for item in range(184,264,2)] + [item for item in range(264,183,-2)]
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import PillowWriter
ims = []
for angle in v_angles:
fig = plt.figure(figsize = (13,8))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z,
cmap=plt.cm.coolwarm,
alpha=0.67,
edgecolor='white',
linewidth=0.25,
zorder=-1)
im = plt.gcf()
ims.append([im])
将它们保存为matplotlib.animation.ArtistAnimation() 对象:
ani = animation.ArtistAnimation(fig,
ims,
interval=50,
blit=True,
repeat_delay=500)
看起来图像 (ims) 确实已被收集并且ani 已正确保存:
In[574]: ims
Out[575]:
[[<Figure size 936x576 with 1 Axes>],
[<Figure size 936x576 with 1 Axes>],
[<Figure size 936x576 with 1 Axes>],
...
In[576]: ani
Out[577]: <matplotlib.animation.ArtistAnimation at 0x107571fa90>
当我尝试创建 .gif 时
writer = PillowWriter(fps=20)
ani.save("3d_scatter.gif", writer='imagemagick')
我收到以下IndexError:
File ".../anaconda3/lib/python3.7/site-packages/matplotlib/animation.py", line 575, in finish
self._frames[0].save(
IndexError: list index out of range
看起来self._frames 应该包含项目,但没有。
有谁知道如何解决这个问题?谢谢。
【问题讨论】:
标签: python matplotlib animation gif index-error