process.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!./env/bin/python
  2. from datetime import datetime, timedelta
  3. import matplotlib.pyplot as plt
  4. from pylab import mpl
  5. # 设置显示中文字体
  6. mpl.rcParams["font.sans-serif"] = ["SimHei"]
  7. # 初始化一个起始日期(这里假设是今天凌晨0点)
  8. start_date = datetime.now().replace(hour=20, minute=21, second=0, microsecond=0)
  9. with open('minicom.cap', 'r') as file:
  10. lines = file.readlines()
  11. times = []
  12. temperatures = []
  13. for line in lines:
  14. time_str, temp_str = line.strip().split('temp = ')
  15. time = start_date + timedelta(seconds=int(time_str.split(':')[2])) + \
  16. timedelta(minutes=int(time_str.split(':')[1])) + \
  17. timedelta(hours=int(time_str.split(':')[0]))
  18. temperature = float(temp_str)
  19. if len(temperatures) > 0:
  20. pre_temp = temperatures[-1]
  21. else:
  22. pre_temp = temperature
  23. if pre_temp-0.5 < temperature < pre_temp+0.5:
  24. times.append(time)
  25. temperatures.append(temperature)
  26. # 确保时间序列是排序的
  27. times.sort()
  28. # 绘制折线图
  29. plt.plot(times, temperatures)
  30. plt.xlabel('时间')
  31. plt.ylabel('温度 (摄氏度)')
  32. plt.title('温度随时间变化图')
  33. plt.xticks(rotation=45) # 旋转x轴标签以更好地展示时间
  34. # 自动调整x轴刻度以显示日期时间格式
  35. plt.gcf().autofmt_xdate()
  36. # 显示图形
  37. plt.savefig("img.png")
  38. plt.show()