Home > database >  How to convert horizontal bar graph to vertical graph?
How to convert horizontal bar graph to vertical graph?

Time:10-13

I am trying to plot the accuracy of all models in bar graph but I am getting horizontal graph instead of vertical graph as you show the axis has been changed. How to correct it? Moreover, I want to print the accuracy score on top of each bar in the given box. Please help me out.

#plotting bar graph of all model's accuracy.

x=['LR','KNN','SVM','DT']
height=[LR_score,KNN_score,SVC_score,DT_score]
acc=  [round(x,2) for x in height] 

plt.barh(x,acc)

plt.xlabel("Algorithms")
plt.ylabel("Accuracy Score")

for index, value in enumerate(acc):
    plt.text(value, index, str(value))
plt.show()

enter image description here

CodePudding user response:

You'e using plt.barh which is a function to exclusively produce horizontal bars, that's why the h at the end of bar (doc here)

For vertical bars the default function is plt.bar (doc here)

CodePudding user response:

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html

import matplotlib.pyplot as plt
x=['LR','KNN','SVM','DT']
height=[LR_score,KNN_score,SVC_score,DT_score]
acc =  [round(x,2) for x in height] 

plt.bar(x,acc)

plt.xlabel("Algorithms")
plt.ylabel("Accuracy Score")

for value, index in enumerate(acc):
    plt.text(value, index, str(acc[value]))
plt.show()
  • Related