你需要:
- 提取kde行的数据
- 对其进行积分以计算累积分布函数(CDF)
找到使CDF等于1/2的值,即中位数
import numpy as np
import scipy
import seaborn as sns
import matplotlib.pyplot as pltsns.set_palette(“hls”, 1)
data = np.random.randn(30)
p=sns.kdeplot(data, shade=True)x,y = p.get_lines()[0].get_data()
care with the order, it is first yinitial fills a 0 so the result has same length than xcdf = scipy.integrate.cumtrapz(y, x, initial=0)
nearest_05 = np.abs(cdf-0.5).argmin()
x_median = x[nearest_05]
y_median = y[nearest_05]plt.vlines(x_median, 0, y_median)
plt.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)