How to map number to color using matplotlib's colormap?

后端 未结 3 986
清酒与你
清酒与你 2020-12-12 16:09

Consider a variable x containing a floating point number. I want to use matplotlib\'s colormaps to map this number to a color, but not plot anything. Basically,

3条回答
  •  盖世英雄少女心
    2020-12-12 16:55

    Number value to colormap color

    import matplotlib.cm as cm
    import matplotlib as matplotlib
    
    def color_map_color(value, cmap_name='Wistia', vmin=0, vmax=1):
        # norm = plt.Normalize(vmin, vmax)
        norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
        cmap = cm.get_cmap(cmap_name)  # PiYG
        rgb = cmap(norm(abs(value)))[:3]  # will return rgba, we take only first 3 so we get rgb
        color = matplotlib.colors.rgb2hex(rgb)
        return color
    

提交回复
热议问题