首页 > 解决方案 > 将概率传递给 seaborn kdeplot

问题描述

假设我想使用以下数据绘制sns.kdeplot

np.random.seed(42)
x = [np.random.randint(0, 10) for _ in range(10)]

x
[6, 3, 7, 4, 6, 9, 2, 6, 7, 4]

在此处输入图像描述

但是现在,假设我有每个值的概率,而不是每个值:

# y is a pd.Series
y
6    0.3
7    0.2
4    0.2
9    0.1
3    0.1
2    0.1

是否有可能kdeplot从这些概率中构建?

我认为 seaborn 可能会计算这些值,因此我认为这是可能的

标签: pythonseaborndata-visualization

解决方案


您应该能够使用(在 v0.11.0 中添加)的weights参数来完成此操作,例如kdeplot

sns.kdeplot(x=x, weights=y)

推荐阅读