【问题标题】:Increasing gif resolution in matplotlib在 matplotlib 中增加 gif 分辨率
【发布时间】:2021-11-10 23:40:26
【问题描述】:

我正在制作 Python matplotlib 动画,我想将其保存为 GIF,但 GIF 分辨率太低。为了将 GIF 分辨率与我想要的质量进行比较,我以 PNG 格式保存了一个帧。结果要好得多,如下所示:

Animated GIF

PNG

这是我的 PNG 绘图代码:

# Plot (PNG)

plt.style.use('dark_background')
plt.figure()
X, Y = np.meshgrid(np.arange(0, Lx), np.arange(0, Ly))
plt.contourf(X, Y, psisquare[3000, :, :], 100, cmap = plt.cm.viridis)
plt.colorbar()
plt.xlabel("x")
plt.ylabel("y")
plt.title("Densité de probabilité à t = " + str(round(3000*dt,5)) + " s")
plt.savefig('2D_Schrodinger_Equation.png')

并为 GIF 绘制代码:

# Plot (GIF)

plt.style.use('dark_background')
fig = plt.figure()

def animate(k):
    k=k*100
    plt.clf()
    plt.pcolormesh(psisquare[k, :, :], cmap = plt.cm.viridis)
    plt.colorbar()
    plt.xlabel("x")
    plt.ylabel("y")
    plt.title(f"Densité de probabilité à t = {k*dt:.5f} s")
    return

anim = animation.FuncAnimation(fig, animate, frames = int(Nbi/100), interval = 50, repeat = True)

writergif = animation.PillowWriter(fps=30)
anim.save("2D_Schrodinger_Equation.gif", writer=writergif)

如何提高 GIF 的质量?谢谢。

【问题讨论】:

  • 您应该提供完整的代码,以便任何人测试它
  • @Nathz,更改 plt.figure() DPI 是否解决了问题?

标签: python matplotlib animation gif dpi


【解决方案1】:

我认为增加图形 dpi 会增加生成动画的 dpi。为了提高质量,请尝试修改您的图形实例化:

fig = plt.figure(dpi = 300)

您可能还需要通过调用animation.PillowWriter.setup() 方法来修改脚本的结尾,并且可能还需要为animation.Animation.save() 方法提供dpi 参数。如果没有您尝试重现的完整代码,我无法确定。

anim = animation.FuncAnimation(fig, animate, frames = int(Nbi / 100), interval = 50, repeat = True)

writergif = animation.PillowWriter(fps = 30)

# fig.dpi is default dpi, but can also be specified explicitly if preferred
writergif.setup(fig, "2D_Schrodinger_Equation.gif", dpi = 300) 

# May or may not need to specify dpi argument
anim.save("2D_Schrodinger_Equation.gif", writer = writergif, dpi = "figure")

查看matplotlib.animation.PillowWriter 文档here 中的setup 方法和matplotlib.animation.Animation 文档here 中的save 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2022-08-22
    • 2022-10-07
    • 1970-01-01
    • 2013-11-29
    • 1970-01-01
    相关资源
    最近更新 更多