I'm plotting some nodal points data animated in time steps as follows:
fig, ax = plt.subplots()
fig.tight_layout()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
def animate(i):
ax.cla()
plt.cla()
ax.set_aspect('equal', 'box')
c = ax.tricontourf(triang, z[:, i], 10, cmap='plasma')
c.set_clim(np.min(z), np.max(z))
plt.colorbar(c, cax=cax)
anim = FuncAnimation(fig, animate, interval=100, frames=nt)
Where z - is nnodes x number_of_timesteps
matrix of nodal values. But as you can see on the picture below, the colorbar range and values does not seem to be fixed. I mean the values assigned to a particular color seems to be fixed, but the color legend is changing in time. I thought c.set_clim(np.min(z), np.max(z))
should fix it, as it takes minimum and maximum nodal values from the whole set of data at every time step, but apparently it does not fix the colorbar. Is there a way to work it out?
You are getting a different colorbar each time because you are not specifying your levels of your contour. Try:
c = ax.tricontourf(triang, z[:, i], 10, cmap='plasma', vmin=-1, vmax=1, levels=np.arange(-1, 1.02, 0.1))