0

I am building a bar graph in Python using Matplotlib and I am trying to display the percentage symbol after each value I added at the top of each bar. So far I have not been able to figure it out. Here is my code and what the bar graph looks like:

df_intr = pd.read_csv('https://cocl.us/datascience_survey_data', 
                  index_col=0)
df_intr=df_intr.sort_values('Very interested',ascending=False)
df_intr_con=(100 * df_intr / 2233)
ax = df_intr_con.plot(kind='bar', figsize=(20, 10), width=.8, color=('#5cb85c', '#5bc0de', 
'#d9534f'), edgecolor=None)
ax.set_title('Percentage of Respondents Interest in Data Science Areas', size=16)
ax.legend(labels=df_intr_con.columns,fontsize= 14)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.tick_params(labelsize=14)
for a in ax.patches:
    ax.annotate(np.round(a.get_height(),decimals=2),
            (a.get_x()+a.get_width()/2, a.get_height()),
            ha='center',
            va='center',
            xytext=(0, 10),
            textcoords='offset points',
            fontsize = 14,
          ),
plt.show()

bar_plot

Smectic
  • 114
  • 1
  • 9
Sn3kyJ3di
  • 3
  • 2

1 Answers1

0

You can convert your values to a string and then add the % sign in the ax.annote

for a in ax.patches:
    txt = np.round(a.get_height(), decimals=2)
    anot = txt.astype('str')
    ax.annotate(anot+'%', (a.get_x()+a.get_width()/2, a.get_height()),
                ha='center', va='center', xytext=(0, 10),
                textcoords='offset points', fontsize=14,)
Smectic
  • 114
  • 1
  • 9