Search code examples
pythonmatplotlibcolorscolorbar

Customize existing color bar


How to move white portion of diverging color bar towards maximum value or minimum value.

import matplotlib.cm as cm
import matplotlib.pyplot as plt
import matplotlib as mpl
norm = mpl.colors.Normalize(vmin=0,vmax=600)
fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cm.PiYG,
                                norm=norm,
                                orientation='horizontal')
cb1.set_label('Some Units')
fig.show()

enter image description here


Solution

  • You have to use the TwoSlopeNorm normalization class

    In [5]: import matplotlib.pyplot as plt 
       ...: import matplotlib as mpl 
       ...:  
       ...: norm = mpl.colors.TwoSlopeNorm(vcenter=450, vmin=0, vmax=600) 
       ...: fig, ax = plt.subplots(figsize=(6, 1)) 
       ...: fig.subplots_adjust(bottom=0.5) 
       ...: cb1 = mpl.colorbar.ColorbarBase(ax, cmap=mpl.cm.PiYG, 
       ...:                                 norm=norm, 
       ...:                                 orientation='horizontal') 
       ...: cb1.set_label('Some Units')    
    

    enter image description here