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

Published: February 10, 2019

DMCA.com Protection Status

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 ?
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 ?
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

Image

of