Python Forum
How to display percentage above the bars in bar graph? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to display percentage above the bars in bar graph? (/thread-25473.html)



How to display percentage above the bars in bar graph? - WhatsupSmiley - Mar-31-2020

Hi there, I am trying to figure out how to display the percentage values above each bar in my bar graph. I am using matplotlib and I created a dataframe that has the percentage.

Here is my code for the dataframe:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt

#sort the dataframe in descending order of Very Interested, Somewhat Interested, and Not Interested
df_VInt = df_TopSurvey.sort_values(by='Very interested', ascending=False)

#Convert the numbers into percentages of the total number of respondents
perc = (df_VInt /2233) *100 
perc = round (perc, 2)
perc
which shows:
Output:
Very interested Somewhat interested Not interested Data Analysis / Statistics 75.59 19.88 2.69 Machine Learning 72.95 21.36 3.31 Data Visualization 60.01 32.87 4.57 Big Data (Spark / Hadoop) 59.65 32.65 5.69 Deep Learning 56.56 34.48 6.09 Data Journalism 19.21 48.41 27.32
Here is the code I have for my bargraph (and what I've tried to get the percentage to show on top of each bar of the graph):
colors = ['#5cb85c','#5bc0de','#d9534f']

perc.plot(kind='bar', figsize=(20,8), width=0.8, color=colors, )
plt.xlabel('Data Science Areas', fontsize=14)
plt.ylabel('Percentage of Respondents', fontsize=14)
plt.title('Percentage of Respondents Interest in Data Science Areas', fontsize=16)

plt.xticks(fontsize=14)
for spine in plt.gca().spines.values():
    spine.set_visible(False)
plt.yticks([])

for p in ax.patches:
    width, height = p.get_width(), p.get_height()
    x,y=p.get_xy()
    ax.annotate('{:.0%}', format(height), (x,y + height + 0.01))
plt.show()
And this is what I get:
İmage