I have an imshow plot with a diverging color map where the data ranges from -1 -> +2 I would like red shading to correspond to positive values and blue to correspond to negative, with white representing zero.
p = plt.imshow(matrix*2,
aspect='auto',
cmap='RdBu_r',
vmin=-2,
vmax=2)
cb = plt.colorbar()
cb.set_label('Temp Gradient ($\degree$C/cm)',fontsize='x-large')
However because I have to set the vmin and vmax symettrically about zero, the colorbar goes down to -2 when it only needs to go down to -1. Is there a way to trim the colorbar so it only shows colors/values in the range -1 -> +2 to match the range of the data?
I note that several posts like this and this purport to answer my question, but actually change the color map rather than the color bar. I just want to change the limits of the bar, but keep the mapping the same.