Using matplotlib to draw color bar with distinguishable and uncontinues colors

前端 未结 1 809
感动是毒
感动是毒 2020-12-03 13:14

The color bar in the figure below is drawn in matlab. I am wondering if it is possible to draw the similar color bar in python/matplotlib? I need the un-smoothing

相关标签:
1条回答
  • 2020-12-03 13:35

    Use ListedColormap and BoundaryNorm:

    import numpy as np
    import matplotlib as mpl
    from matplotlib import pyplot
    data = np.floor(np.random.random((10,10)) * 6)
    cmap = mpl.colors.ListedColormap(['w', 'b', 'g', 'y', '#ff8c00', 'r'])
    norm = mpl.colors.BoundaryNorm([0,1,2,3,4,5,6], cmap.N)
    pyplot.imshow(data, cmap=cmap, norm=norm, interpolation='none')
    pyplot.colorbar()
    pyplot.show()
    

    Some examples of using ListedColormap: colorbars, multicolored lines.

    0 讨论(0)
提交回复
热议问题