Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color the shaded area under the curve distribution plot different colors

I'm using seaborn's kdeplot to draw the distribution of my data.

sns.kdeplot(data['numbers'], shade=True)

I want to divide the shaded area under the line into three parts, showing the "high" percentile and the "low" percentile. It would be ideal if I can color the shaded area with three different colors.

Any idea how I can go about doing that?

I want it to look something like the below where I can decide the cutoff value between the colors.

enter image description here

like image 566
BKS Avatar asked Mar 07 '23 20:03

BKS


1 Answers

So I figured out how to do it. I would retrieve and x and y arrays from the seaborn plot, then use fill_between to color under the curve.

points = sns.kdeplot(data['numbers'], shade=True).get_lines()[0].get_data()

x = points[0]
y = points[1]

plt.fill_between(x,y, where = x >=0.75, color='r')
plt.fill_between(x,y, where = x <=0.1, color='g')
plt.fill_between(x,y, where = (x<=0.75) & (x>=0.1), color='y')
like image 155
BKS Avatar answered Mar 11 '23 04:03

BKS