python matplotlib 多簇柱状图实例

python matplotlib 多簇柱状图:

统计机器人15天搬运量与机台产出对比,上下货方式为机器人+人工

import xlrd
import matplotlib.pyplot as plt
import numpy as np

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

#打开Excel读取数据
data = xlrd.open_workbook( r"F:/PYTHON/ROBOT_15_days_MOVE.xls")
sheet1=data.sheet_by_index(0)

#读取列数据,sheet1.col_values(0)第一行为标题
date1=sheet1.col_values(0)[1:]
r_qty=sheet1.col_values(1)[1:]
e_qty=sheet1.col_values(2)[1:]

#处理空数据
for a in r_qty:
    if a=='':
        a=0
for a in e_qty:
    if a=='':
        a=0
#将数据格式化为int类型
r_qty = list(map(int, r_qty))
e_qty = list(map(int, e_qty))

#获取date1长度,图表x轴坐标
r=range(len(date1))
x=np.arange(len(date1))

#设置柱状图宽度
bar_width=0.35

#柱状图-第一列 alpha:透明度
plt.bar(x=range(len(date1)),height=r_qty,label='RBT_MOVE',alpha=0.8,color='Blue',width=bar_width)

plt.bar(x=np.arange(len(date1))+bar_width,height=e_qty,label='EQP MOVE',alpha=0.8,color='orange',width=bar_width)

#柱状图-添加标签
for x,y in enumerate(r_qty):
    plt.text(x,y+100,'%s' % y,ha='center',va='bottom')
for x,y in enumerate(e_qty):
    plt.text(x+bar_width,y+100,'%s' % y,ha='center',va='top') 

x=list(range(len(date1)))
print(x)

plt.ylim([0,5000])#y轴坐标范围
plt.xticks(x,date1,rotation=45)#x轴设置,斜45度
plt.title("ROBOT 15 DAYS MOVE")
plt.xlabel('date')
plt.ylabel('move')
plt.legend()#图列
#plt.show()

#保存输出图片
plt.savefig(r"F:/PYTHON/ROBOT_15_days_MOVE.png")

结果显示:

posted @ 2021-04-02 14:04  bellin124  阅读(931)  评论(0编辑  收藏  举报