title: Set bespoke colorbar range in matplotlib
body: <p>I have an imshow plot with a diverging color map where the data ranges from -1 -&gt; +2 I would like red shading to correspond to positive values and blue to correspond to negative, with white representing zero.</p>
  <p><a href="https://i.stack.imgur.com/FU8KM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FU8KM.png" alt="enter image description here" /></a></p>
  <pre><code>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')
  </code></pre>
  <p>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 -&gt; +2 to match the range of the data?</p>
  <p>I note that several posts like <a href="https://www.kite.com/python/answers/how-to-set-colorbar-range-using-matplotlib-in-python#:%7E:text=To%20set%20a%20Colorbar%20range,upper%20bound%20for%20color%20scaling." rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/3373256/set-colorbar-range-in-matplotlib/3376734">this</a> 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.</p>