How to extract html color codes (hex codes) from a seaborn palette ?

Active February 10, 2019    /    Viewed 13900    /    Comments 0    /    Edit


To extract html color codes from a seaborn palette, a solution is to use the method as_hex():

How to extract html color codes (hex codes) from a seaborn palette ?

>>> import seaborn as sns
>>> pal = sns.color_palette("Blues")
>>> print(pal.as_hex())

returns

['#dbe9f6', '#bad6eb', '#89bedc', '#539ecd', '#2b7bba', '#0b559f']

Hexadecimal code can be then be use in a matplotlib figure fro example:

How to extract html color codes (hex codes) from a seaborn palette ?

import matplotlib.pyplot as plt
import numpy as np

x_min = 0.0
x_max = 12.0

x = np.linspace(x_min, x_max, 100)

y = np.linspace(x_min, x_max, 100)

plt.plot(x,y, color='black')

plt.xlim(0,12)
plt.ylim(0,12)

plt.title("How to extract code color from a seaborn palette ?",fontsize=10)

ptx = np.linspace(0, 2, 10)
pty = ptx
plt.fill_between(ptx, pty, color='#dbe9f6', alpha='1.0')

ptx = np.linspace(2, 4, 10)
pty = ptx
plt.fill_between(ptx, pty, color='#bad6eb', alpha='1.0')

ptx = np.linspace(4, 6, 10)
pty = ptx
plt.fill_between(ptx, pty, color='#89bedc', alpha='1.0')

ptx = np.linspace(6, 8, 10)
pty = ptx
plt.fill_between(ptx, pty, color='#539ecd', alpha='1.0')

ptx = np.linspace(8, 10, 10)
pty = ptx
plt.fill_between(ptx, pty, color='#2b7bba', alpha='1.0')

ptx = np.linspace(10, 12, 10)
pty = ptx
plt.fill_between(ptx, pty, color='#0b559f', alpha='1.0')

plt.savefig("extract_seaborn_palette.png")
plt.show()

References

Links Site
seaborn color palette seaborn
Python - sns.color_palette output to Hex number for Bokeh stackoverflow
Extract RGB or 6 digit code from Seaborn palette stackoverflow
seaborn sequential color palettes seaborn


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!



Did you find this content useful ?, If so, please consider donating a tip to the author(s). MoonBooks.org is visited by millions of people each year and it will help us to maintain our servers and create new contents.

Amount