Active 10 février 2019    /    Viewed 1777    /    Comments 0    /    Edit

Il est possible d'extraire le code couleur html (hexadécimal) d'une palette seaborn en utilisant la méthode as_hex()

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

donne

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

On peut alors utiliser ces codes dans une figure matplotlib, exemple:

````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()`
```