Seaborn:如何绘制一条与累积 KDE 中的特定 y 值相匹配的垂直线?

nev*_*ves 2 distribution matplotlib seaborn

我正在使用 Seaborn 绘制累积分布,它是使用以下代码的 KDE:

sns.distplot(values, bins=20, 
             hist_kws= {'cumulative': True}, 
             kde_kws= {'cumulative': True} )
Run Code Online (Sandbox Code Playgroud)

这给了我下面的图表:

累积分布和 kde

我想绘制一条垂直线和相应的 x 索引,其中 y 为 0.8。就像是:

KDE 带有垂直线

如何获取特定 y 的 x 值?

Joh*_*anC 5

您可以在 80% 分位数处画一条垂直线:

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

values = np.random.normal(1, 20, 1000)
sns.distplot(values, bins=20,
             hist_kws= {'cumulative': True},
             kde_kws= {'cumulative': True} )
plt.axvline(np.quantile(values, 0.8), color='r')
plt.show()
Run Code Online (Sandbox Code Playgroud)

示例图