how to add data Labels to seaborn countplot / fact

2019-04-16 00:48发布

问题:

I use python3 , seaborn countplot , my question :

  1. how to add the count values for everybar? Show the label at the top of each bar
  2. how to make these bars in descending order ?

i wrote this:

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

enter image description here

Thanks a lot !

回答1:

I used a simple example data I generated but you can replace the df name and column name to your data:

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))

This generates:

You will be likely to play around with the location a little bit to make it look nicer.