如何为分类属性创建概率分布图?

2024-06-10 04:13:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如何为分类属性创建概率分布图?你知道吗

我试图使用sns.distplot,但得到错误:TypeError: unsupported operand type(s) for /: 'str' and 'int'。你知道吗

然而,当我运行sns.countplot(x="WAKE", data=df, palette="Greens_d")时,我得到了一个正确的计数图。你知道吗

数据框=

ID   WAKE
1    H
2    H
3    L
4    H
5    M
6    M
7    H
8    L

Tags: andfor属性type错误分类intsns
1条回答
网友
1楼 · 发布于 2024-06-10 04:13:40

不是严格意义上的海生,但我希望基本Matplotlib可以。你知道吗

您可以将类别缩减为数字并绘制直方图。然后可以使用此直方图作为密度直方图对其进行规格化。我强烈要求你们阅读这里每一个功能的文档。你知道吗

df = pd.DataFrame()
df['Wake'] = ['H', 'H', 'L', 'H', 'M', 'M', 'H', 'L']

# Reduce categories to numbers
vals = df['Wake'].values
uniq, idx = np.unique(vals, return_inverse=True)

# View results as groups (just for show)
df['C'] = idx
df.groupby('Wake').count()

# Substract 0.5 to center to the indices
i = idx - 0.5
plt.hist(i, bins=np.arange(0, idx.max()+2, 1)-0.5, density=True)
plt.show()

相关问题 更多 >