Pyplot colorbar not consistent when animating colormesh

31 Views Asked by At

I have tried various methods of animating a colormesh plot. I believe that FuncAnimation is the simplest way for me to animate my colormesh. The code I have written is shown below

r = np.linspace(0,R,Nr)
z = np.linspace(0,H,Nz)

fig, ax = plt.subplots()

cax = ax.pcolormesh(R, Z, T[0,:,:], cmap='RdBu_r', vmin=0, vmax=200)
ax.set_title("Variation of temperature with space" )
ax.set_xlabel("r (m)")
ax.set_ylabel("z (m)")
fig.colorbar(cax)

def update(i):
    cax = set_array(T[i,:,:])
    ax.set_title("Variation of temperature with space after a time of " + str(i*dt_real) + " seconds" )
    return cax
ani = animation.FuncAnimation(fig,update,interval = 100, frames=len(T)-1)
ani.save('123.gif')
plt.show()

In my data r and z are arrays representing dimensions. T is a 3d matrix. Index 1 is time, 2 is z, and 3 is r. I want to animate the colormesh of T for each time step t.

I have had no problems generating a single colormesh. When I animate the colormesh, the gif generated does not replicate the series of colormeshes if I generated them one by 1. The colorbar appears to flicker between frames and is constantly changing throughout the animation. I have looked online and can find no other example of this occuring. My code almost directly replicates other versions which do not face the same problem (See example here)

Gif generated. Note flickering on the colorbar

Solution:

Flickering and variation disappeared when creating an mp4 file as opposed to gif.

0

There are 0 best solutions below