Custom shade palette in kdeplot seaborn

Viewed 738

I am trying to custom my shade color in my kdeplot.

I would like to use this palette, strating from a light green to the #3bd6b0 color with at least 15 levels (but maybe more later). In order to do that, I use the sns.light_palette like this :

sns.light_palette('#3bd6b0',15)

this works very well and corespond to this palette : palette

but when I am running this code :

sns.set_palette(sns.light_palette('#3bd6b0', 15))

x = np.random.normal(10, 5, 20)
y = np.random.normal(10, 5, 20)

sns.kdeplot( x=x, y=y, shade=True, shade_lowest=False,cbar=True, thresh= 0.0001, n_levels=15)

I get this :

kdeplot

Which is not what I want...
I tried many things with the parameters cmap and palette, but nothing works.

Can you help me and tell me what it's wrong in my code.

Many thanks

1 Answers

The kdeplot either creates its own shades from a given fixed color, or you can explicitly set a colormap. If you provide nothing, matplotlib's color cycle is used. In your case, kdeplot would create its own color ramp from the first of the first color encountered (which is the lightest of the colors in the palette). The color ramp goes to dark if the given color is considered light, and goes to white when the given color is considered dark.

You can provide the colormap via sns.kdeplot(..., cmap=...). sns.colorpalette(..., as_cmap=True) converts a seaborn palette to a colormap.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

x = np.random.normal(10, 5, 20)
y = np.random.normal(10, 5, 20)

cmap = sns.light_palette('#3bd6b0', as_cmap=True)
ax = sns.kdeplot(x=x, y=y, shade=True, cbar=True, thresh=0.0001, n_levels=15, cmap=cmap)
plt.show()

2d kdeplot with custom shades

Related