从 Seaborn 调色板中提取 RGB 或 6 位代码 [英] Extract RGB or 6 digit code from Seaborn palette

查看:46
本文介绍了从 Seaborn 调色板中提取 RGB 或 6 位代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Seaborn 可以选择创建漂亮的调色板.我希望使用这些调色板来生成在地图中可以很好地协同工作的颜色,其中根据某些属性对国家/地区进行着色.下面的代码产生了 8 种从浅到深的紫色阴影.另请注意,需要能够指定颜色数量,因此我不能只使用已定义颜色的固定调色板.

Seaborn has an option to create nice color palettes. I wish to use these palettes to generate colors that work well together in a map where countries are shaded according to some property. The following code produces 8 shades of purple from light to dark. Note also the ability to specify the number of colors is required so I cannot just use a fixed palette of defined colors.

import seaborn as sns
num_shades = 8
sns.palplot(sns.cubehelix_palette(num_shades))

如果我运行相同但在这样的列表中:

If I run the same but in a list like so:

color_list = sns.cubehelix_palette(num_shades)

你得到:

[[0.9312692223325372, 0.8201921796082118, 0.7971480974663592], ... 

这些显然不是我需要的 RGB 值.

These are clearly not RGB values which is what I need.

1) 这些颜色是什么格式?2) 如何转换为 RGB 或 6 位代码?

1) What format are these colors in? 2) How can I convert to RGB or 6 digit codes?

我已经尝试搜索了很长时间,但没有找到答案.我看过这里和其他 seaborn 文档:

I have tried searching for quite some time and found no answers. I have looked here and at other seaborn documentation:

https://stanford.edu/~mwaskom/software/seaborn/generated/seaborn.set_color_codes.html

我可以使用以下方法将 RGB 转换为 6 位代码:

I can convert to 6 digit codes from RGB using:

转换 RGB 颜色元组到一个六位数的代码,在 Python 中

但我不知道如何直接或通过获取 RGB 值来完成.任何帮助将不胜感激.

but am stuck as to how to do it direct or via getting the RGB values. Any help would be appreciated.

推荐答案

您得到的值是 255 的百分比,即最大 RGB 值.只需将每个三元组值乘以 255(如果您愿意,还可以四舍五入)即可获得 RGB 值.

The values you are getting are percentages of 255, the max RGB value. Just multiply each triplet of values by 255 (and round off, if you like) to get the RGB values.

for color in color_list:
    for value in color:
        value *= 255

然后将它们存储在一个新列表中以获得您的 RGB 值列表.

Then store those in a new list to have your list of RGB values.

这篇关于从 Seaborn 调色板中提取 RGB 或 6 位代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆