2017-10-12 145 views
1

我使用seaborn的kdeplot绘制我的数据分布。颜色在曲线分布下的阴影区域绘制不同的颜色

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

我想将线下的阴影区域分为三部分,显示“高”百分位和“低”百分位。如果我可以用三种不同颜色为阴影区域着色,那将是理想的。

任何想法我可以怎么做呢?

我希望它看起来像下面这样我可以决定颜色之间的截止值。

enter image description here

+0

我不确定seaborn有直接的命令来填充曲线。但是你可以使用matplotlib(如果你有seaborn,你也有matplotlib,因为它有一个依赖)来做到这一点。检查[fill_between示例](https://matplotlib.org/examples/pylab_examples/fill_between_demo.html)。 – armatita

+0

@armatita我已经看了fill_between。事情是这是一个分配,所以它就像一个历史纪录。 y是自动生成的。我只是给它的x值。它不一定是seaborn,用matplotlib生成类似的东西也很好。 – BKS

+0

它是一个非常高水平的阴谋,但它在matplolib中完全可重复。我的猜测是seaborn [kdeplot](https://seaborn.pydata.org/generated/seaborn.kdeplot.html)正在使用[scipy.stats.gaussian_kde](https://docs.scipy.org/doc/scipy /reference/generated/scipy.stats.gaussian_kde.html)。它确实有点复杂,但没有那么多。 – armatita

回答

1

所以我想通了,如何做到这一点。我将从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') 
+0

如果解决了您的问题,请不要忘记接受您的答案。 –