python - 如何使用 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 包来做到这一点?
解决方案
我解决了转换为 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进行了检查)。你知道这是什么原因以及如何解决它吗?
推荐阅读
- node.js - 节点 - 在浏览器中下载现有的 excel
- html - 我的“ngSubmit”有时会出错
- kubernetes - 使用 Istio ServiceEntry 的域注册
- c# - 从 MS SQL 数据库获取数据到 Xamarin Android App Gridview
- python - 在 Python 模块而不是 __main__ 的上下文中执行 REPL 代码?
- email - 在 gsuite 中为不同的客户添加不同的 logo
- javascript - 使用JS水平滚动HTML表格
- arrays - DB 结果集后的慢速数组操作
- icinga2 - 如何修复 icinga2.service:主要进程已退出
- java - 使用 Yaml 文件的随机数据生成器