9

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"]
Hamid K
  • 983
  • 1
  • 18
  • 40

2 Answers2

21

Even better (assuming seaborn >= 0.6):

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

['#fdd4c2', '#fca082', '#fb694a', '#e32f27', '#b11218']
mwaskom
  • 46,693
  • 16
  • 125
  • 127
4

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']
Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
  • Is the output of this command "sns.color_palette('Reds', 5)" RGB? if yes, should not be in the range of 0 to 256? – Hamid K Oct 28 '15 at 15:58
  • 1
    That depends on the definition of what RGB is. For as far as i know, `matplotlib` assumes RGB floats to be between 0 and 1, and RGB integers between 0 and 255. If you invert the transformation with `mpl.colors.hex2color` you will end up with the original values again. – Rutger Kassies Oct 28 '15 at 16:04