Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 835
Pete Avatar asked Jul 07 '16 15:07

Pete


People also ask

What is SNS Color_palette?

sns. color_palette("pastel") Return a specified number of evenly spaced hues in the “HUSL” system: sns.


2 Answers

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

pal = sns.color_palette(...) pal.as_hex() 
like image 132
mwaskom Avatar answered Sep 23 '22 01:09

mwaskom


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.

like image 40
perfect5th Avatar answered Sep 23 '22 01:09

perfect5th