小编Sta*_*Man的帖子

将动画 Matplotlib 图形保存为 GIF 文件会产生不同的绘图

我使用FuncAnimationMatplotlib Animation 类创建了一个动画绘图,我想将其保存为 .gif 文件。当我运行脚本时,输出看起来正常,如下所示(动画工作正常):

在此输入图像描述

但是,当我尝试使用 ImageMagick 或 PillowWriter 将动画图另存为 .gif 文件时,该图如下图所示:

在此输入图像描述

线条明显粗了很多,总的来说,看起来很糟糕。该问题归因于点(紫色和红色圆圈)。因此,似乎情节是在每一帧上书写的(我认为是这样)。我可以通过将它们全部删除来避免这种情况。但我不想这样做,因为很难看清线条。

这是代码:

line, = ax.plot([], [], color = 'blue', lw=1)
line2, = ax.plot([], [], color = 'red', lw=1)
line3, = ax.plot([], [], color = 'purple', lw=1)
def animate(i):
    line.set_data(x1[:i], y1[:i])
    line2.set_data(x2[:i], y2[:i])
    line3.set_data(x3[:i], y3[:i])
    point1, = ax.plot(x1[i], y1[i], marker='.', color='blue')
    point2, = ax.plot(x2[i], y2[i], marker='.', color='red')
    point3, = ax.plot(x3[i], y3[i], marker='.', color='purple')
    return line, line2, line3, point1, point2, point3,
        
ani = animation.FuncAnimation(fig, animate, interval=20, blit=True, repeat=False, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib animated-gif matplotlib-animation

12
推荐指数
1
解决办法
2万
查看次数