1
以下代碼繪製了可交互更改其值的2D數組。唯一缺少的是我希望它改變一下colobar。不知何故,當我修改圖像時,顏色條不會自行調整。如何更新交互式繪圖中的顏色條
from pyplot import *
from numpy import *
from matplotlib.widgets import Slider, Button, RadioButtons
ax = subplot(111)
subplots_adjust(left=0.25, bottom=0.25)
my_img=random((100,100))*10.
l = imshow(my_img,origin='lower');cbar=colorbar()
ax1= axes([0.25, 0.1, 0.65, 0.03], axisbg='w')
par1 = Slider(ax1, 'normalization', 1, 100., valinit=1.5)
def update(val):
new_normalization = par1.val
new_img=random((100,100))*new_normalization
l.set_array(new_img)
# this doesn't change the new colobar maximum
cbar.vmax=new_img.max()
cbar.vmin=0
draw()
par1.on_changed(update)
resetax = axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color='w', hovercolor='0.975')
def reset(event):
par1.reset()
button.on_clicked(reset)
show()