You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将matplotlib水平柱状图改为垂直柱状图并在柱顶标注准确率

修改方案

你当前使用的plt.barh是Matplotlib提供的水平柱状图绘制接口,改成垂直柱状图的调整点如下:

  • 将绘图接口替换为垂直柱状图专用的plt.bar
  • 修正坐标轴标签的对应关系
  • 调整柱顶数值标注的坐标参数,适配垂直布局

完整修改后代码

# 绘制所有模型的准确率柱状图
x=['LR','KNN','SVM','DT']
height=[LR_score,KNN_score,SVC_score,DT_score]
acc=  [round(x,2) for x in height] 

# 替换barh为bar绘制垂直柱状图
plt.bar(x,acc)

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

# 调整标注坐标,ha='center'让文本水平居中在柱子上方,va='bottom'避免文本和柱子重叠
for index, value in enumerate(acc):
    plt.text(index, value, str(value), ha='center', va='bottom')
plt.show()

内容的提问来源于stack exchange,提问作者meet delvadiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 05:27:01