plot.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from ctypes import Structure
  2. from re import M
  3. import pandas as pd
  4. import numpy as np
  5. import datetime
  6. import time, datetime
  7. import matplotlib.pyplot as plt
  8. from pylab import*
  9. from scipy.signal import savgol_filter
  10. from sklearn.linear_model import LinearRegression
  11. import os
  12. import log
  13. def get_file(): #创建一个空列表
  14. files =os.listdir(path)
  15. files.sort() #排序
  16. file_list= []
  17. for file in files:
  18. if not os.path.isdir(path +file): #判断该文件是否是一个文件夹
  19. f_name = str(file)
  20. # print(f_name)
  21. tr = '\\' #多增加一个斜杠
  22. filename = path + tr + f_name
  23. file_list.append(filename)
  24. return file_list
  25. mylog=log.Mylog('log_diag.txt','error')
  26. mylog.logcfg()
  27. fig = plt.figure(figsize=(20,15))
  28. path = r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\02析锂检测\02析锂分析\各项目析锂情况_211130\增加波峰高度与峰谷比计算结果\析锂前后五电池的soh'
  29. file_list = get_file()
  30. a1 = []
  31. b1 = []
  32. for file in file_list:
  33. df_time = []
  34. data = pd.read_csv(file,encoding='GB18030')
  35. # fig = plt.figure(figsize=(20,10))
  36. df_fig = data
  37. df_fig = df_fig.reset_index(drop=True)
  38. length = len(df_fig)
  39. df_soh_temp = df_fig['soh']
  40. df_soh = savgol_filter(df_soh_temp, 11, 3)
  41. df_hms_temp = pd.to_datetime(df_fig['time_sp'])#转换时间格式
  42. # df_hms = df_hms_temp.dt.time#仅保留时分秒
  43. df_hms_fig = df_hms_temp.apply(lambda x:x.strftime('%Y-%m-%d'))
  44. temp = file.split('\\')
  45. sn_name = temp[-1].split('.')[0]
  46. title = sn_name
  47. time_reshape = np.array(df_hms_temp).reshape(-1,1)
  48. soh_reshape = df_soh.reshape(-1,1)
  49. delta_time = (np.diff(df_hms_temp)/pd.Timedelta(1, 'days'))#计算时间差
  50. df_time_len = len(delta_time)
  51. for i in range(df_time_len):
  52. df_time.append(sum(delta_time[0:i]))
  53. soh_reshape = np.delete(soh_reshape, [-1])
  54. # b1.append(lineModel.intercept_[0])#对应sn
  55. #新建一个线性回归模型,并把数据放进去对模型进行训练
  56. lineModel = LinearRegression()
  57. lineModel.fit(np.array(df_time).reshape(-1,1), soh_reshape)
  58. # a1.append(lineModel.coef_[0][0])#拟合系数
  59. fit_soh = lineModel.predict(np.array(df_time).reshape(-1,1))
  60. fit_soh = 100*fit_soh/fit_soh[0]
  61. # plt.scatter([x for x in range(0, length)], df_fig[volt_num][0:length], label=volt_num)
  62. plt.plot(df_time, fit_soh,linewidth = 2, linestyle = '-', marker = 's',label=title)
  63. mpl.rcParams['font.sans-serif']=['KaiTi']
  64. mpl.rcParams['axes.unicode_minus']=False #用来正常显示负号
  65. plt.legend(fontsize=20)
  66. plt.xlabel('时间(天)', fontsize=26)
  67. plt.ylabel('soh随时间演化', fontsize=26)
  68. plt.xticks(range(1,len(df_time),5),rotation=45, fontsize=26)
  69. plt.yticks(fontsize=26)
  70. # plt.xlim((df_hms_temp[0]-datetime.timedelta(minutes=5)).dt.time,(df_hms_temp[0] + datetime.timedelta(hours=1.5)).dt.time)
  71. # plt.ylim(-2.5,0.5)
  72. plt.title('析锂量前5与后5的电池包soh演化', fontsize=28)
  73. # plt.show()
  74. plt.savefig(r'D:\Work\Code_write\data_analyze_platform\USER\lzx\01算法开发\02析锂检测\02析锂分析\各项目析锂情况_211130\增加波峰高度与峰谷比计算结果\soh图片\\'+'析锂量前5与后5电池包的soh.png', dpi=300)