【发布时间】:2021-11-10 23:40:26
【问题描述】:
我正在制作 Python matplotlib 动画,我想将其保存为 GIF,但 GIF 分辨率太低。为了将 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