Reputation: 35
I am making a Python matplotlib animation and I'd like to save it as a GIF, but the GIF resolution is too low. To compare the GIF resolution to my desired quality, I saved a single frame in PNG format. The result is much better, as you can see below:
Here is my plotting code for the 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')
And plotting code for the 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)
How can I improve the GIF quality? Thank you.
Upvotes: 2
Views: 2476
Reputation: 146
I think increasing the figure dpi will increase the dpi of the resulting animation. For improved quality try modifying your figure instantiation:
fig = plt.figure(dpi = 300)
You may also need to modify the end of your script with a call to the animation.PillowWriter.setup()
method, and possibly also providing the dpi
argument to the animation.Animation.save()
method. Without your full code to attempt to reproduce, I can't be sure.
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")
Check out the setup
method in the matplotlib.animation.PillowWriter
documentation here and the save
method in the matplotlib.animation.Animation documentation here.
Upvotes: 2