Extract RGB or 6 digit code from Seaborn palette

Pete picture Pete · Jul 7, 2016 · Viewed 15.8k times · Source

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.

Answer

mwaskom picture mwaskom · Jul 8, 2016

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

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