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")
結果顯示: