Search code examples
matplotlibcolorbar

How to make a colorbar given a list of values and the corresponding key of hex values in matplotlib?


I'm just trying to just want a colorbar to represent these values using the following colors.

vals=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 49, 50, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 69, 78, 81, 84, 91, 139, 187, 203, 296]

cols=['#b3b3b3', '#fefe6d', '#fefb6a', '#fefb6a', '#fefb6a', '#fef968', '#fef968', '#fef968', '#fef665', '#fef665', '#fef665', '#fef462', '#fef462', '#fef462', '#fef25f', '#fef25f', '#fef25f', '#fef05c', '#fef05c', '#fef05c', '#feed5a', '#feed5a', '#feed5a', '#feeb57', '#feeb57', '#feeb57', '#fee954', '#fee954', '#fee954', '#fee751', '#fee751', '#fee751', '#fee54f', '#fee54f', '#fee54f', '#fee34c', '#fee34c', '#fee34c', '#fee149', '#fee149', '#fee047', '#fee047', '#fede44', '#fede44', '#fede44', '#fedc42', '#fedc42', '#feda3f', '#feda3f', '#fed83d', '#fed83d', '#fed83d', '#fed63b', '#fed63b', '#fed438', '#fed438', '#fed438', '#fed236', '#fed236', '#fed236', '#fed034', '#fed034', '#fece31', '#fece31', '#fec82b', '#fec529', '#fec327', '#febd22', '#fc940d', '#ec6504', '#de5502', '#652200']


Solution

  • You could create a custom colormap using ListedColormap together with a BoundaryNorm. Note that you need one boundary more than the number of colors.

    import matplotlib.pyplot as plt
    from  matplotlib.cm import ScalarMappable
    from  matplotlib.colors import ListedColormap, BoundaryNorm
    import numpy as np
    
    vals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 43, 44, 45, 46, 47, 49, 50, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 65, 66, 67, 69, 78, 81, 84, 91, 139, 187, 203, 296]
    cols = ['#b3b3b3', '#fefe6d', '#fefb6a', '#fefb6a', '#fefb6a', '#fef968', '#fef968', '#fef968', '#fef665', '#fef665', '#fef665', '#fef462', '#fef462', '#fef462', '#fef25f', '#fef25f', '#fef25f', '#fef05c', '#fef05c', '#fef05c', '#feed5a', '#feed5a', '#feed5a', '#feeb57', '#feeb57', '#feeb57', '#fee954', '#fee954', '#fee954', '#fee751', '#fee751', '#fee751', '#fee54f', '#fee54f', '#fee54f', '#fee34c', '#fee34c', '#fee34c', '#fee149', '#fee149', '#fee047', '#fee047', '#fede44', '#fede44', '#fede44', '#fedc42', '#fedc42', '#feda3f', '#feda3f', '#fed83d', '#fed83d', '#fed83d', '#fed63b', '#fed63b', '#fed438', '#fed438', '#fed438', '#fed236', '#fed236', '#fed236', '#fed034', '#fed034', '#fece31', '#fece31', '#fec82b', '#fec529', '#fec327', '#febd22', '#fc940d', '#ec6504', '#de5502', '#652200']
    bounds = np.append(vals, vals[-1] + 1)
    
    cmap = ListedColormap(cols)
    norm = BoundaryNorm(bounds, ncolors=len(cols))
    
    fig, ax = plt.subplots(figsize=(12, 1))
    fig.subplots_adjust(bottom=0.5)
    fig.colorbar(ScalarMappable(norm=norm, cmap=cmap),
                 cax=ax, orientation='horizontal', label='Colorbar')
    plt.show()
    

    ListedColormap with BoundaryNorm

    Or using proportional spacing between the colors:

    fig.colorbar(ScalarMappable(norm=norm, cmap=cmap), ticks=np.arange(0, 300, 25),
                 cax=ax, orientation='horizontal', spacing='proportional')
    

    proportional spacing

    If you need a smooth colormap, you could use LinearSegmentedColormap.from_list().

    from  matplotlib.colors import LinearSegmentedColormap
    
    cmap = LinearSegmentedColormap.from_list('', list(zip(np.array(vals)/vals[-1], cols)))
    norm = plt.Normalize(vals[0], vals[-1])
    
    fig.colorbar(ScalarMappable(norm=norm, cmap=cmap), ticks=np.arange(0, 300, 25),
                 cax=ax, orientation='horizontal')
    

    LinearSegmentedColormap.from_list