Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - sns.color_palette output to Hex number for Bokeh

Tags:

hex

seaborn

bokeh

What is the output format of

sns.color_palette('Reds', 5)?

How can I convert it to Hex numbers?

Running the above command gives you the Output of:

[(0.99358708227381987, 0.83234141714432663, 0.76249136363758763), (0.98823529481887817, 0.62614381313323975, 0.50849674145380652), (0.98357554884517895, 0.4127950837799147, 0.28835064675293715), (0.89019608497619629, 0.18562091638644537, 0.15294117977221808), (0.69439448188332953, 0.070034602810354785, 0.092318341048324815)] 

How can I convert that to an output like below:

["#9b59b6", "#3498db", "#95a5a6", "#e74c3c", "#34495e", "#2ecc71", "#43A16F"]
like image 823
Hamid K Avatar asked Oct 28 '15 15:10

Hamid K


2 Answers

Even better (assuming seaborn >= 0.6):

import seaborn as sns
pal = sns.color_palette('Reds', 5)
pal.as_hex()

['#fdd4c2', '#fca082', '#fb694a', '#e32f27', '#b11218']
like image 70
mwaskom Avatar answered Sep 23 '22 11:09

mwaskom


Matplotlib has a rgb2hex function:

rgb_colors = sns.color_palette('Reds', 5))
hex_colors = list(map(mpl.colors.rgb2hex, rgb_colors)

Results in:

['#fdd4c2', '#fca082', '#fb694a', '#e32f27', '#b11218']
like image 39
Rutger Kassies Avatar answered Sep 23 '22 11:09

Rutger Kassies