Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
407 views
in Technique[技术] by (71.8m points)

python - Extract RGB or 6 digit code from Seaborn palette

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)

you get:

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

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

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

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

I can convert to 6 digit codes from RGB using:

Converting a RGB color tuple to a six digit code, in Python

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If by "6 digit code" you mean a hex code, you can also do:

pal = sns.color_palette(...)
pal.as_hex()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...