I am trying to update the colorbars in my plot. Unfortunately, only the colors update i.e. the tick values do not change only the colors of the bar change accorgingly with the current values in the contour plot. I would like to make the ticks change as well as the colors in the colorbar.
import matplotlib
import numpy as np
import pylab as py
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
fig, axs = plt.subplots(1, 2)
# I define the variables below but do not give the exact values as they come from computations
x - one dimensional array
v - four dimensional array
v_mag - three dimensional array
T_string - three dimensional array
X, Y = np.meshgrid(x, x)
cs1 = axs[0].quiver(X, Y, v[0][0], v[0][1],v_mag[0], cmap=cm.seismic)
cs2 = axs[1].contourf(X, Y, T[0], 100)
cbar1=fig.colorbar(cs1, ax=axs[0], format=\"%.2f\")
cbar2=fig.colorbar(cs2, ax=axs[1], format=\"%.2f\")
axcolor = 'lightgoldenrodyellow'
time = py.axes([0.1, 0.01, 0.65, 0.03], axisbg = axcolor)
S_time = Slider(time, 'Time', 0, 50, valinit = 0);
def update(val) :
timeval = int(S_time.val)
cs1.set_UVC(v[timeval][0],v[timeval][1], v_mag[timeval])
cbar1.on_mappable_changed(cs1)
cs2 = axs[1].contourf(X, Y, T[timeval], 100)
cbar2.on_mappable_changed(cs2)
plt.show()
#second try
def update(val) :
timeval = int(S_time.val)
cs1.set_UVC(v[timeval][0],v[timeval][1], v_mag[timeval])
cbar1.on_mappable_changed(cs1)
cs2 = axs[1].contourf(X, Y, T[timeval], 100)
cbar2.set_clim( np.amin(np.array(T[timeval])) , np.amax(np.array(T[timeval])) )
cbar2.update_ticks()
cbar2.draw_all()
plt.draw()
S_time.on_changed(update)
plt.show()
The answer is to define the update function as:
def update(val) :
timeval = int(S_time.val)
cs1.set_UVC(v[timeval][0],v[timeval][1], v_mag[timeval])
cbar1.on_mappable_changed(cs1)
cs2 = axs[1].contourf(X, Y, T[timeval], 100, cmap=cm.jet)
cbar2.set_ticklabels(np.linspace(np.amin(np.array(T[timeval])),np.amax(np.array(T[timeval])), num = 12))
cbar2.update_ticks()
plt.show()