Python Forum
How to display percentage above the bars in bar graph?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to display percentage above the bars in bar graph?
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to create a plot with line graphs and vertical bars devansing 6 2,251 Feb-28-2023, 05:38 PM
Last Post: devansing
  Comparing and Identifying ID with Percentage jonatasflausino 1 2,407 Jun-23-2020, 06:44 PM
Last Post: hussainmujtaba
  error bars with dataframe and pandas Hucky 4 4,145 Apr-27-2020, 02:02 AM
Last Post: Hucky
  display graph in columns and rows william888 1 1,823 Jul-02-2019, 10:19 AM
Last Post: dataman
  percentage change mean by group SriRajesh 1 3,156 Jan-09-2019, 01:49 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020