[英]Color the shaded area under the curve distribution plot different colors
I'm using seaborn's kdeplot to draw the distribution of my data.我正在使用 seaborn 的 kdeplot 来绘制我的数据分布。
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.
如果我可以用三个不同的 colors 为阴影区域着色,那将是理想的。
Any idea how I can go about doing that?知道我怎么能 go 这样做吗?
I want it to look something like the below where I can decide the cutoff value between the colors.我希望它看起来像下面这样我可以决定 colors 之间的截止值。
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. 我将从seaborn图中检索x和y数组,然后使用
fill_between
为曲线下的颜色着色。
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')
The only difference since the original answer was posted is that shade=True
needs to be changed to shaded=False
.自发布原始答案以来的唯一区别是
shade=True
需要更改为shaded=False
。
I also just wanted to share a slightly more complete answer for those who want to copy-paste.我也只是想为那些想要复制粘贴的人分享一个稍微完整的答案。
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
mean = 0
std = 1
np.random.seed(0)
x = np.random.normal(loc=mean, scale=std, size=10000)
points = sns.kdeplot(x, shade=False, bw_method=1.0, color='black').get_lines()[0].get_data()
x = points[0]
y = points[1]
plt.fill_between(x, y, color='y')
plt.fill_between(x, y, where = (x<=0.5*std) & (x >= -0.5 * std), color='g') #
plt.fill_between(x, y, where = (x>=2*std) | (x<=-2*std), color='r')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.