0

Below is the code that I am using to generate the bar plots and I want to display the respective values above the plot:

import numpy as np
import folium
import pandas as pd
import matplotlib.pyplot as plt
from numpy.ma.bench import xl
from wordcloud import WordCloud,STOPWORDS
import matplotlib as mp
import seaborn as sns

df=pd.read_csv("Topic_Survey_Assignment.csv",index_col=0) # reading csv file
df.sort_values("Very interested", ascending = False)
df_p=(df/2233)*100
df_p=round(df_p,2)
xlist=[]
ylist=[]
c=["#5cb85c","#5bc0de","#d9534f"]
a=df_p.plot(kind='bar',figsize=(20,8),color=c)     # plotting bar plot
plt.xlabel('Subjects',fontsize=14)
plt.ylabel('Percentage of People',fontsize=14)
plt.title('Data Science Statistics',fontsize=20)
plt.legend(fontsize=14)

plt.show(a)
print(xlist)

Plot Image

enter image description here

Nihal
  • 5,262
  • 7
  • 23
  • 41

1 Answers1

2

You can add annotations to the bar plot like this:

for p in ax.patches:
    ax.annotate(str(p.get_height()), (p.get_x() + 0.08, p.get_height()), va='bottom', ha='center')

Here is an example with randomly generated data:

# Generate sample data
c=["#5cb85c","#5bc0de","#d9534f"]
fig, ax = plt.subplots()
df_p.plot(ax = plt.gca(), kind='bar',figsize=(12,8),color=c)     # plotting bar plot
plt.xlabel('Subjects',fontsize=14)
plt.ylabel('Percentage of People',fontsize=14)
plt.title('Data Science Statistics',fontsize=20)
plt.legend(fontsize=14)
plt.tight_layout()

for p in ax.patches:
    ax.annotate(str(p.get_height()), (p.get_x()+0.08, p.get_height()),
                va='bottom', ha='center', weight='bold')

Bar plot with annotations

Nathaniel
  • 3,230
  • 11
  • 18