Is this your expected output?

If so, credit to Chris Adams for his answer.
I predefined the values for accu_dt , accu_svm, accu_rf, accu_lg, accu_knn, accu_nb
as such:
accu_dt = [0.90,0.92,0.91,0.95,0.99,0.95,0.90]
accu_svm = [0.89,0.92,0.95,0.98,0.97,0.89,0.95]
accu_rf = [0.98,0.99,0.97,0.96,0.98,0.99,0.95]
accu_lg = [0.79,0.77,0.90,0.85,0.83,0.80,0.78]
accu_knn = [0.85,0.85,0.84,0.89,0.83,0.81,0.80]
accu_nb = [0.85,0.84,0.83,0.81,0.85,0.85,0.85]
I didn't change much of your code except the width = 0.7
and the figsize=(30,7)
to make the numbers more readable. Noticed that I added loop at the bottom of your code
ax = results.plot(kind = 'bar',figsize=(30,7),color=['black','gray','brown','blue','pink','green'],width = 0.7)
ax.set_title('Comparision of Models',fontsize=15)
ax.set_yticks([0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9])
ax.set_xticklabels(names ,fontsize=15,rotation = 45)
ax.set_xlabel("Models",fontsize=15)
ax.set_ylabel("Accuracy",fontsize=15)
# Newly Added Loop
for p in ax.patches:
width = p.get_width()
height = p.get_height()
x, y = p.get_xy()
ax.annotate(f'{height:.0%}', (x + width/2, y + height*1.02), ha='center')
Basically what the code does in simple term:
- Getting the heights, width and (x,y) coordinate of the bars
- Set the annotation at designated (x,y) coordinate
- Set the VALUE of the annotation equals the height of the bar (the
'.0%'
means percentage with zero decimal place)