首页 > 解决方案 > 如何使用 matplotlib/seaborn 获得亮度保持灰度的调色板

问题描述

我试图了解特定调色板在保持亮度的灰度(例如打印时)中的外观。我正在使用为色盲人士安全定义的颜色生成调色板:

import matplotlib as mpl
import seaborn as sns

# http://mkweb.bcgsc.ca/colorblind
cblind = [mpl.colors.to_hex((213/255, 94/255, 0/255)),
          mpl.colors.to_hex((86/255, 180/255, 233/255)),
          mpl.colors.to_hex((.9, .9, .9))]

### how to convert this palette to grayscale?
cblind_gray =
###

pal = sns.color_palette(cblind)
sns.palplot(pal)

如何使用 matplotlib/seaborn 或其他 python 包来做到这一点?

标签: pythonmatplotlibcolorsseaborncolor-palette

解决方案


我解决了转换为 HSV 的问题,将饱和度设置为 0 并重新转换回 rgb:

# http://mkweb.bcgsc.ca/colorblind
rgbs = [(213/255, 94/255, 0/255), (86/255, 180/255, 233/255), (.9, .9, .9)]

cblind = [mpl.colors.to_hex(r) for r in rgbs]
cblind_gray = [mpl.colors.to_hex(
    mpl.colors.hsv_to_rgb(mpl.colors.rgb_to_hsv(r) * (1,0,1)))
    for r in rgbs]

pal = sns.color_palette(cblind)
sns.palplot(pal)

pal = sns.color_palette(cblind_gray)
sns.palplot(pal)

这是正确的方法吗?

更新

显然它不起作用(第二种颜色在放入灰度时是不同的 - 我用Color Oracle进行了检查)。你知道这是什么原因以及如何解决它吗?


推荐阅读