Python获取任意渐变色区段的16进制色值列表
背景 在玩转可视化的过程中,matplotlib及任意的其他可视化库,都有自己的cmap生成器,然而,有些时候,可能需要根据列表的长度预生成一组渐变色。 代码 def gen_colors(N): values = [int(i*250/N) for i in range(N)] # print(values) colors=["#%02x%02x%02x"%(200,int(g),40)for g in values] # 250 250 250 ,g值越小越靠近0红色 return colors colors = gen_colors(df['2020年'].shape[0]) colors 在Bokeh中进行渲染测试: x = y = [i for i in range(21)] p=figure() p.scatter(x,y,radius=0.2, fill_color=colors, fill_alpha=1.0, line_color=None ) show(p) 如果你想要其他色盘中的某一端色值: from PIL import Image # 16进制颜色格式颜色转换为RGB格式 def Hex_to_RGB(hex): r = int(hex[1:3],16) g = int(hex[3:5],16) b = int(hex[5:7], 16) return r