2022年 11月 5日

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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