I use python3, seaborn countplot, my question :
I wrote this:
fig = plt.figure(figsize=(10,6))
sns.countplot(data_new['district'],data=data_new)
plt.show()
Thanks a lot !
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.
I know it's an old question, but I guess there is a bit easier way of how to label a seaborn.countplot
or matplotlib.pyplot.bar
than in previous answer here (tested with matplotlib-3.4.2 and seaborn-0.11.1).
ax = sns.countplot(x=df['feature_name'],
order=df['feature_name'].value_counts(ascending=False).index);
abs_values = df['feature_name'].value_counts(ascending=False).values
ax.bar_label(container=ax.containers[0], labels=abs_values)
Example:
ax = sns.countplot(x=df['feature_name'],
order=df['feature_name'].value_counts(ascending=False).index);
abs_values = df['feature_name'].value_counts(ascending=False)
rel_values = df['feature_name'].value_counts(ascending=False, normalize=True).values * 100
lbls = [f'{p[0]} ({p[1]:.0f}%)' for p in zip(abs_values, rel_values)]
ax.bar_label(container=ax.containers[0], labels=lbls)
Example:
Check these links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With