Extract RGB or 6 digit code from Seaborn palette

Question:

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.

Asked By: Pete

||

Answers:

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

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

Answered By: perfect5th

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

pal = sns.color_palette(...)
pal.as_hex()
Answered By: mwaskom
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.