0

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.

enter image description here

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.

Robbie Mallett
  • 131
  • 1
  • 11
  • Check out this answer to a related question: https://stackoverflow.com/questions/13784201/matplotlib-2-subplots-1-colorbar/64707057#64707057 take note of a comment to that question. – Bjoern Dahlgren Apr 07 '21 at 08:32
  • Yes unfortunately that is not relevant. It is possible to run normalizer=Normalize(-1,2) then im=cm.ScalarMappable(norm=normalizer,cmap='RdBu_r') and then cb = plt.colorbar(im). This produces a colorbar with limits of -1 -> 2 (I want this) but it does not maintain the required red-blue color mapping that is symmetrical about zero. Instead it is symmetrical about 0.5. As such the method is no different to calling plt.clim(-1,2). That is to say, it alters the mapping rather than just simply setting the limits of the colorbar. – Robbie Mallett Apr 07 '21 at 08:47
  • I think you'll need to create your own colormap with grey pffset to 1/3 instead of 1/2 then? – Bjoern Dahlgren Apr 08 '21 at 10:08

0 Answers0