我正在尝试更新我的情节中的颜色条。不幸的是,只有颜色更新,即刻度值不会仅改变条的颜色,而是与等高线图中的当前值一致地改变。我想更改滴答以及颜色栏中的颜色。
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()
答案是将更新功能定义为:
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()