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

时间:2018-03-01 06:54:06

标签: python-3.x seaborn

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

  1. 如何为每个栏添加计数值?在顶部显示标签 每个酒吧
  2. 如何按降序制作这些条形图?
  3. 我写道:

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

    enter image description here

    非常感谢!

1 个答案:

答案 0 :(得分: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

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