首页 > 解决方案 > Seaborn:如何在累积的 KDE 中绘制与特定 y 值匹配的垂直线?

问题描述

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

sns.distplot(values, bins=20, 
             hist_kws= {'cumulative': True}, 
             kde_kws= {'cumulative': True} )

这给了我以下图表:

累积分布和kde

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

带竖线的 KDE

如何获得特定 y 的 x 值?

标签: matplotlibseaborndistribution

解决方案


您可以在 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()

示例图


推荐阅读