如何将数据标签添加到seaborn countplot / factorplot

问题描述 投票:2回答:1

我使用python3,seaborn countplot,我的问题:

  1. 如何为每个栏添加计数值?在每个栏的顶部显示标签
  2. 如何按降序排列这些条形图?

我写了这个:

fig = plt.figure(figsize=(10,6))
sns.countplot(data_new['district'],data=data_new)
plt.show()

enter image description here

非常感谢 !

python-3.x seaborn
1个回答
5
投票

我使用了生成的简单示例数据,但您可以将df名称和列名替换为您的数据:

ax = sns.countplot(df["coltype"], 
                   order = df["coltype"].value_counts().index)

for p, label in zip(ax.patches, df["coltype"].value_counts().index):
    ax.annotate(label, (p.get_x()+0.375, p.get_height()+0.15))

这会产生:enter image description here

您可能会稍微调整一下位置以使其看起来更漂亮。

© www.soinside.com 2019 - 2024. All rights reserved.