Bottom Page

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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  error bars with dataframe and pandas Hucky 4 288 Apr-27-2020, 02:02 AM
Last Post: Hucky
  display graph in columns and rows william888 1 455 Jul-02-2019, 10:19 AM
Last Post: dataman
  percentage change mean by group SriRajesh 1 956 Jan-09-2019, 01:49 PM
Last Post: scidam

Forum Jump:


Users browsing this thread: 1 Guest(s)