python画条形图

python画条形图,可以根据自己的需求修改

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

# 输入统计数据
x_lable = ('站立', '静坐', '平躺', '行走', '上楼', '下楼')
x_train_number = [1190, 1175, 800, 1200, 1380, 1360]
x_test_number = [580, 410, 400, 590, 570, 610]

bar_width = 0.2  # 条形宽度
index_x_train = np.arange(len(x_lable))  # 训练集条形图的横坐标
index_x_test = index_x_train + bar_width  # 测试集条形图的横坐标

# 使用两次 bar 函数画出两组条形图
plt.bar(index_x_train, height=x_train_number, width=bar_width, color='#0066CC', label='训练集')
plt.bar(index_x_test, height=x_test_number, width=bar_width, color='#339966', label='测试集')

plt.legend(loc="upper left", prop={"size": 12, })  # 显示图例  设置图例的大小和方向

plt.yticks(fontsize=16, color='#000000')  # 改变纵坐标刻度的大小
plt.xticks(index_x_train + bar_width / 2, x_lable, size=16)  # 让横坐标轴刻度显示 x_lable中的动作类型, index_x_train + bar_width/2 为横坐标轴刻度的位置

# plt.ylabel(' ')  # 纵坐标轴标题
# plt.title(' ')  # 图形标题

plt.show()

效果如下:
在这里插入图片描述图中的横坐标纵坐标数值以及字体都可以根据自己的需求修改

  • 13
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值