# How to show percentage (text) next to the horizontal bars in matplotlib? [closed]

I am using seaborn's countplot to show count distribution of 2 categorical data. Fine it works but I want the percentages to show on top of the bars for each of the plot. Please how do I do it?

fig, ax = plt.subplots(1, 2)
sns.countplot(y = df['current_status'], ax=ax[0]).set_title('Current Occupation')
sns.countplot(df['gender'], ax=ax[1]).set_title('Gender distribution')


I have made edits based on the comments made but I can't get the percentages to the right of horizontal bars. This is what I have done.

total = len(df['current_status'])*1.
ax = sns.countplot(y="current_status", data=df)
plt.title('Distribution of  Configurations')
plt.xlabel('Number of Axles')

for p in ax.patches:
ax.annotate('{:.1f}%'.format(100*p.get_height()/total), (p.get_y()+0.1, p.get_height()+5))
_ = ax.set_xticklabels(map('{:.1f}%'.format, 100*ax.xaxis.get_majorticklocs()/total))


• stackoverflow.com/questions/33179122/… – Frayal Mar 26 '19 at 15:00
• @Alexis, please I have made edits to the question. – radioactive Mar 26 '19 at 15:55
• @Esmailian I have made some edits but I still have some issues. Please take a look. – radioactive Mar 26 '19 at 15:55
• It is better to say "I want the percentages to the right of horizontal bars" – Esmailian Mar 26 '19 at 15:58
• @Esmailian I have edited it Sir. So whats the solution? – radioactive Mar 26 '19 at 16:06

Here is a working example to add a text to the right of horizontal bars:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

df = pd.DataFrame(np.array([['a'], ['a'], ['b']]), columns=['current_status'])
ax = sns.countplot(y="current_status", data=df)
plt.title('Distribution of  Configurations')
plt.xlabel('Number of Axles')

total = len(df['current_status'])
for p in ax.patches:
percentage = '{:.1f}%'.format(100 * p.get_width()/total)
x = p.get_x() + p.get_width() + 0.02
y = p.get_y() + p.get_height()/2
ax.annotate(percentage, (x, y))

plt.show()


Output:

You can move the texts around by changing the formula for x and y. For example, to place them on top:

x = p.get_x() + p.get_width() / 2
y = p.get_y() - 0.02


Here is a general guideline for annotating axes.

• Thank you for your assistance. – radioactive Mar 26 '19 at 17:24
# plot1 real

fig, ax= plt.subplots(figsize =(12,40))
sns.set_style('whitegrid')
sns.set_context('notebook')
#sns.set(font_scale=2)
plt.fontsize =(35)

ax=sns.barplot( palette="Dark2",ax=ax,

x='square km', y='state',data=d)
total=len(d['state'])
for p in ax.patches:
percentage ='{:,.0f}KM²'.format(p.get_width())
width, height =p.get_width(),p.get_height()
x=p.get_x()+width+0.02
y=p.get_y()+height/2
ax.annotate(percentage,(x,y))

plt.xticks(rotation =50,fontsize =18)
plt.yticks(rotation =50,fontsize =20)

plt.title('NIGERIA\'S 36 STATES & FCT RANKED\n IN ORDER OF LAND SURFACE AREA (KM²)',
fontsize =30
)
plt.xlabel ('SQUARE KM',fontsize =24)
plt.ylabel ('STATE',fontsize =24)

plt.show ()


• Hi, welcome to Data Science StackExchange. Please consider improving your answer by adding a comment to the suggested code. – Romain Reboulleau Nov 12 '19 at 18:41