Search code examples
pythonmatplotlibcolorsgifcolorbar

How can I use matplotlib to create a GIF whose frames share a commonly-scaled colorbar?


I want to make a GIF animation of a wave propagation. My problem is the following: the colorbar should be constant for all the frames. How to make sure that the scale of the solution can be maintained from one frame to another, for the sake of readability?

Here is my code:

fig = plt.figure(figsize=(7,7))
fig = dolfin.plot(u)

plt.title(r'$u(\mathbf{x}, t)$')
plt.xlabel(r'$x$ [m]')
plt.ylabel(r'$y$ [m]')
plt.colorbar(fig, fraction=0.058, pad=0.04)
plt.savefig('fig'+str(n)+'.png',bbox_inches='tight', dpi=150)
plt.close()

Solution

  • As per my comments, you should just be able to add plt.clim(vmin,vmax), or fig.set_clim(vmin,vmax), as necessary.

    Reference: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.clim.html