Map values to colors in matplotlib

前端 未结 2 1768
臣服心动
臣服心动 2020-12-14 18:08

I have a list of numbers as follows:

lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173, 
       1.2427632053442292, 1.1809971732733273, 0.919         


        
2条回答
  •  离开以前
    2020-12-14 18:37

    Colormaps are powerful, but (a) you can often do something simpler and (b) because they're powerful, they sometimes do more than I expect. Extending mfitzp's example:

    import matplotlib
    import matplotlib.cm as cm
    
    lst = [1.9378076554115014, 1.2084586588892861, 1.2133096565896173, 1.2427632053442292,
       1.1809971732733273, 0.91960143581348919, 1.1106310149587162, 1.1106310149587162,
       1.1527004351293346, 0.87318084435885079, 1.1666132876686799, 1.1666132876686799]
    
    minima = min(lst)
    maxima = max(lst)
    
    norm = matplotlib.colors.Normalize(vmin=minima, vmax=maxima, clip=True)
    mapper = cm.ScalarMappable(norm=norm, cmap=cm.Greys)
    
    for v in lst:
        print(mapper.to_rgba(v))
    
    # really simple grayscale answer
    algebra_list = [(x-minima)/(maxima-minima) for x in lst]
    # let's compare the mapper and the algebra
    mapper_list = [mapper.to_rgba(x)[0] for x in lst]
    
    matplotlib.pyplot.plot(lst, mapper_list, color='red', label='ScalarMappable')
    matplotlib.pyplot.plot(lst, algebra_list, color='blue', label='Algebra')
    
    # I did not expect them to go in opposite directions. Also, interesting how
    # Greys uses wider spacing for darker colors.
    # You could use Greys_r (reversed)
    
    # Also, you can do the colormapping in a call to scatter (for instance)
    # it will do the normalizing itself
    matplotlib.pyplot.scatter(lst, lst, c=lst, cmap=cm.Greys, label='Default norm, Greys')
    matplotlib.pyplot.scatter(lst, [x-0.25 for x in lst], marker='s', c=lst,
                          cmap=cm.Greys_r, label='Reversed Greys, default norm')
    matplotlib.pyplot.legend(bbox_to_anchor=(0.5, 1.05))
    

    values of normed colors

提交回复
热议问题