Seaborn:如何在累积 KDE 中绘制与特定 y 值匹配的垂直线?
Seaborn: how to draw a vertical line that matches a specific y value in a cumulative KDE?
我正在使用 Seaborn 绘制累积分布,它是 KDE 使用此代码:
sns.distplot(values, bins=20,
hist_kws= {'cumulative': True},
kde_kws= {'cumulative': True} )
这给了我以下图表:
我想绘制一条垂直线和相应的 x 索引,其中 y 为 0.8。类似于:
如何获取特定 y 的 x 值?
您可以在 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()
可能是最好的。我走了另一条路,这可能是一个更通用的解决方案。
思路是获取kde线的坐标,然后求出越过阈值的点的索引
values = np.random.normal(size=(100,))
fig = plt.figure()
ax = sns.distplot(values, bins=20,
hist_kws= {'cumulative': True},
kde_kws= {'cumulative': True} )
x,y = ax.lines[0].get_data()
thresh = 0.8
idx = np.where(np.diff(np.sign(y-thresh)))[0]
x_val = x[idx[0]]
ax.axvline(x_val, color='red')
我正在使用 Seaborn 绘制累积分布,它是 KDE 使用此代码:
sns.distplot(values, bins=20,
hist_kws= {'cumulative': True},
kde_kws= {'cumulative': True} )
这给了我以下图表:
我想绘制一条垂直线和相应的 x 索引,其中 y 为 0.8。类似于:
如何获取特定 y 的 x 值?
您可以在 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()
思路是获取kde线的坐标,然后求出越过阈值的点的索引
values = np.random.normal(size=(100,))
fig = plt.figure()
ax = sns.distplot(values, bins=20,
hist_kws= {'cumulative': True},
kde_kws= {'cumulative': True} )
x,y = ax.lines[0].get_data()
thresh = 0.8
idx = np.where(np.diff(np.sign(y-thresh)))[0]
x_val = x[idx[0]]
ax.axvline(x_val, color='red')