如何将水平条形图转换为垂直图形?



我试图在条形图中绘制所有模型的准确性,但我得到的是水平图形,而不是垂直图形,因为您显示轴已更改。如何纠正?此外,我想在给定框中的每个栏的顶部打印精度分数。请帮帮我。

#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()

输入图片描述

你使用plt.barh专门生产水平的酒吧是一个函数,这就是为什么hbar年底(doc)

对于竖条,默认函数是plt.bar(文档在这里)

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()