"""Excel数据处理和可视化Version: 0.1Author: weiyl4Date: 2023年10月20日"""import pandas as pdimport numpy as npimport matplotlib.pyplot as pltimport osfrom datetime import datetime# Get the absolute path of the Excel FileExcelPath = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + '\DataProcessAndVisualization\DataDir\\'class ExcelToDict:""" 通过Excel生成DataFrame数据 """def __init__(self,Excel_name,Sheet_name):self.ExcelFilename = Excel_nameExcelfilePath = ExcelPath + Excel_name + '.xlsx'self.RawDataFrame = pd.read_excel(ExcelfilePath,Sheet_name)self.BottomTemp = []self.WallTemp = []self.TempDiff = []self.data_Convert()def get_key_data(self):KeyDataFrame = pd.DataFrame({'底部温度':self.BottomTemp , '侧面温度':self.WallTemp})return KeyDataFramedef add_ColumnName(self):self.RawDataFrame.columns = ['数据源','头码1','头码2','数据长度','底部温度','侧面温度','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','功率','校验']def data_Convert(self):#数据清洗:当一行中有任意缺失值时删除整行self.RawDataFrame = self.RawDataFrame.dropna(how='any')#添加列名self.add_ColumnName()self.BottomTemp = self.RawDataFrame['底部温度'].tolist()self.WallTemp = self.RawDataFrame['侧面温度'].tolist()for index, listdata in enumerate(self.BottomTemp):self.BottomTemp[index] = int(listdata, 16)for index, listdata in enumerate(self.WallTemp):self.WallTemp[index] = int(listdata, 16)for index in range(min(len(self.BottomTemp), len(self.WallTemp))):self.TempDiff.append(self.BottomTemp[index] - self.WallTemp[index])@staticmethoddef Get_AverageDifferOnFIFO(Templist:list, BUFFSIZE: int, SampleTime: int):'''温度缓存10 采样周期5'''TempDiff = []TempDiff.append(0)for index in range(len(Templist)//SampleTime-2):TempBuffPre = 0TempBuffNew = 0for ix in range(BUFFSIZE):TempBuffPre += Templist[ix + SampleTime*index]TempBuffNew += Templist[ix + SampleTime*(index+1)]# TempBuffPre = TempBuffPre/BUFFSIZE# TempBuffNew = TempBuffNew/BUFFSIZETempDiff.append((TempBuffNew - TempBuffPre))return TempDiffdef data_Visualization(self):# 生成一些示例数据x = np.arange(len(self.BottomTemp))# 创建一个图形和两个y轴fig, ax1 = plt.subplots()ax2 = ax1.twinx()#绘制折线图line1 = ax1.plot(x, self.BottomTemp,label='底部温度', color='royalblue', marker='.', ls='-', markersize = 1)line2 = ax1.plot(x, self.WallTemp,label='侧面温度', color='lightseagreen', marker='.', ls='-', markersize = 1)line3 = ax2.plot(x, self.TempDiff, label='温度差分', color='tomato', marker='.', ls='-', markersize = 1)# 设置x轴和y轴的标签,指明坐标含义ax1.set_xlabel('时间', fontdict={'size': 16})ax1.set_ylabel('温度',fontdict={'size': 16})ax2.set_ylabel('温度差分',fontdict={'size': 16})plt.axvline(x=2100, color='r')# print(self.BottomTemp[2110])# print(self.WallTemp[2110])# print(self.TempDiff[2110])#添加图表题plt.title('底部温度-侧面温度曲线')#添加图例lines = line1 + line2 + line3labels = [h.get_label() for h in lines]plt.legend(lines, labels, loc='upper left')# 设置中文显示plt.rcParams['font.sans-serif']=['SimHei']# 使用savefig函数保存程序作出的图像# 语法:plt.savefig(文件名,dpi=图像质量)plt.savefig(ExcelPath + self.ExcelFilename + '.png',dpi=600)#展示图片plt.show()def DataFromExcel(Excel_name, Sheet_name):ExcelData1 = ExcelToDict(Excel_name, Sheet_name)keydf1 = ExcelData1.get_key_data()ExcelData1.data_Visualization()def GetDiffDataFromExcel(Excel_name, Sheet_name):ExcelData1 = ExcelToDict(Excel_name, Sheet_name)WallTempList = (ExcelData1.get_key_data())['侧面温度'].tolist()WallTempDiffList = ExcelData1.Get_AverageDifferOnFIFO(WallTempList, BUFFSIZE = 10, SampleTime=10)print(len(WallTempList))print(len(WallTempDiffList))# 生成一些示例数据x1 = np.arange(len(WallTempList))# 使用列表推导式建立等比列表x2 = [(10 * i) for i in range(len(WallTempDiffList))]# 创建一个图形和两个y轴fig, ax1 = plt.subplots()ax2 = ax1.twinx()#绘制折线图# line1 = ax1.plot(x1, keydf1_BottomTemp,label='底部温度1', color='blue', marker='.', ls='-', markersize = 1)line2 = ax1.plot(x1, WallTempList,label='侧面温度1', color='lightseagreen', marker='.', ls='-', markersize = 1)# line3 = ax2.plot(x1, keydf1_TempDiff, label='温度差分1', color='tomato', marker='.', ls='-', markersize = 1)# line4 = ax1.plot(x2, keydf2_BottomTemp,label='底部温度2', color='cornflowerblue', marker='.', ls='-', markersize = 1)# line5 = ax1.plot(x2, keydf2_WallTemp,label='侧面温度2', color='turquoise', marker='.', ls='-', markersize = 1)line6 = ax2.plot(x2, WallTempDiffList, label='温度差分2', color='salmon', marker='.', ls='-', markersize = 1)plt.axvline(x=2100, color='r')plt.show()'''# 设置x轴和y轴的标签,指明坐标含义ax1.set_xlabel('时间', fontdict={'size': 16})ax1.set_ylabel('温度',fontdict={'size': 16})ax2.set_ylabel('温度差分',fontdict={'size': 16})#添加图表题plt.title('底部温度-侧面温度曲线')#添加图例# lines = line1 + line2 + line3 + line4 + line5 + line6lines = line2 + line6labels = [h.get_label() for h in lines]plt.legend(lines, labels, loc='upper left')# 设置中文显示plt.rcParams['font.sans-serif']=['SimHei']# 使用savefig函数保存程序作出的图像# 语法:plt.savefig(文件名,dpi=图像质量)# plt.savefig(ExcelPath + 'now'+ '.png',dpi=600)#展示图片plt.show()'''def MuitiPlot():# now=datetime.strptime("4/1/23-17-20", "%d/%m/%y %H:%M")ExcelData1 = ExcelToDict("7. 传感器高-3L", "DataLog")keydf1 = ExcelData1.get_key_data()ExcelData2 = ExcelToDict("无锅盖-3L", "DataLog")keydf2 = ExcelData2.get_key_data()# ExcelData2.data_Visualization()keydf1_BottomTemp = keydf1['底部温度'].tolist()keydf1_WallTemp = keydf1['侧面温度'].tolist()keydf1_TempDiff = []for index in range(min(len(keydf1_BottomTemp), len(keydf1_WallTemp))):keydf1_TempDiff.append(keydf1_BottomTemp[index] - keydf1_WallTemp[index])keydf2_BottomTemp = keydf2['底部温度'].tolist()keydf2_WallTemp = keydf2['侧面温度'].tolist()keydf2_TempDiff = []for index in range(min(len(keydf2_BottomTemp), len(keydf2_WallTemp))):keydf2_TempDiff.append(keydf2_BottomTemp[index] - keydf2_WallTemp[index])# 生成一些示例数据x1 = np.arange(len(keydf1_BottomTemp))x2 = np.arange(len(keydf2_BottomTemp))# 创建一个图形和两个y轴fig, ax1 = plt.subplots()ax2 = ax1.twinx()#绘制折线图line1 = ax1.plot(x1, keydf1_BottomTemp,label='底部温度1', color='blue', marker='.', ls='-', markersize = 1)line2 = ax1.plot(x1, keydf1_WallTemp,label='侧面温度1', color='lightseagreen', marker='.', ls='-', markersize = 1)line3 = ax2.plot(x1, keydf1_TempDiff, label='温度差分1', color='tomato', marker='.', ls='-', markersize = 1)line4 = ax1.plot(x2, keydf2_BottomTemp,label='底部温度2', color='cornflowerblue', marker='.', ls='-', markersize = 1)line5 = ax1.plot(x2, keydf2_WallTemp,label='侧面温度2', color='turquoise', marker='.', ls='-', markersize = 1)line6 = ax2.plot(x2, keydf2_TempDiff, label='温度差分2', color='salmon', marker='.', ls='-', markersize = 1)# 设置x轴和y轴的标签,指明坐标含义ax1.set_xlabel('时间', fontdict={'size': 16})ax1.set_ylabel('温度',fontdict={'size': 16})ax2.set_ylabel('温度差分',fontdict={'size': 16})#添加图表题plt.title('底部温度-侧面温度曲线')#添加图例lines = line1 + line2 + line3 + line4 + line5 + line6labels = [h.get_label() for h in lines]plt.legend(lines, labels, loc='upper left')# 设置中文显示plt.rcParams['font.sans-serif']=['SimHei']# 使用savefig函数保存程序作出的图像# 语法:plt.savefig(文件名,dpi=图像质量)plt.savefig(ExcelPath + 'now'+ '.png',dpi=600)#展示图片plt.show()if __name__ == '__main__':GetDiffDataFromExcel("TestDrop",Sheet_name="DataLog")# DataFromExcel("TestDrop",Sheet_name="DataLog")# MuitiPlot()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。