序言
这里想跟大家分享的一个技术点是matplotlib动态展示折线图,效果如下
背景
工作中有一个业务需求,需要采集公司的产品在不同场景下不同部位的温度,比如在产品使用过程中用户接触到的产品表面的温度,产品不同部位采集到的温度需要低于规定值,超过规定值可能就会对用户有烫伤的风险,基于这个项目我接触了Fluke这款温度采集仪器,这个设备市场售价大概5~10W人民币,总的来说是一款非常可靠和精密的仪器,它通过把热偶线粘贴在产品的不同部位每隔1秒记录产品对应位置的温度,并生成相应的报告,背景是这么个背景,细节不便多说。
复盘这个项目,用Fluke这个设备实现了我们项目的需求,但是我也有不满意的地方,第一个是许多功能其实我们是用不上的,相当于我们花了10W经费只用了其中1W经费的功能,浪费了9W,第二它的二次开发接口对我来说不是很友好,因为两边用的不同的技术,中间转了好几道才配合上。
所以就自己做下面这个温度采集仪,经测试,测试结果和Fluke这款设备精度几乎差不多,使用在产品的日常温度采集和创客研究上面毫无压力,有需要可以私信咨询。
matplotlib折线图代码实现
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
import random
import time
# 界面类
class ZheXianTuClass(QWidget):
def __init__(self):
super(ZheXianTuClass,self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('折线图 v1.0.20221208.01-机器人迈克猫')
plt.rcParams['font.sans-serif'] = ['SimHei'] # 解决无法显示中文
plt.rcParams['axes.unicode_minus'] = False # 解决无法显示负号
self.fig = plt.figure()
self.canvas = FC(self.fig)
self.layout = QVBoxLayout() # 设置布局,将组件添加到布局中
self.layout.addWidget(self.canvas)
self.setLayout(self.layout)
self.setGeometry(50,50,1366,768)
# 线程类
class ZheXianTuThread(QThread):
remain_duration_signal=pyqtSignal(list)
def __init__(self):
super(ZheXianTuThread,self).__init__()
def run(self):
while True:
# 生成12条线的随便数
temp_list=[]
for i in range(2):
temp_list.append(random.randint(20,23))
for i in range(2):
temp_list.append(random.randint(24,27))
for i in range(2):
temp_list.append(random.randint(28,31))
for i in range(2):
temp_list.append(random.randint(32,35))
for i in range(2):
temp_list.append(random.randint(36,39))
for i in range(2):
temp_list.append(random.randint(40,43))
# 发送到主界面
self.remain_duration_signal.emit(temp_list)
time.sleep(1)
# 操作类
class ZheXianTuOperate(ZheXianTuClass):
def __init__(self):
super(ZheXianTuOperate,self).__init__()
self.temp_1=[]
self.temp_2=[]
self.temp_3=[]
self.temp_4=[]
self.temp_5=[]
self.temp_6=[]
self.temp_7=[]
self.temp_8=[]
self.temp_9=[]
self.temp_10=[]
self.temp_11=[]
self.temp_12=[]
self.main_ui_start_object=ZheXianTuThread()
self.main_ui_start_object.remain_duration_signal.connect(self.updateRemainDuration)
self.main_ui_start_object.start()
def updateRemainDuration(self,remain_duration_list):
self.temp_1.append(float(remain_duration_list[0]))
self.temp_2.append(float(remain_duration_list[1]))
self.temp_3.append(float(remain_duration_list[2]))
self.temp_4.append(float(remain_duration_list[3]))
self.temp_5.append(float(remain_duration_list[4]))
self.temp_6.append(float(remain_duration_list[5]))
self.temp_7.append(float(remain_duration_list[6]))
self.temp_8.append(float(remain_duration_list[7]))
self.temp_9.append(float(remain_duration_list[8]))
self.temp_10.append(float(remain_duration_list[9]))
self.temp_11.append(float(remain_duration_list[10]))
self.temp_12.append(float(remain_duration_list[11]))
if len(self.temp_1)>1:
self.temp_x=[]
for i in range(len(self.temp_1)-1):
self.temp_x.append(i+1)
plt.cla() # 清空画布
plt.plot(self.temp_x, self.temp_1[1:], linewidth=1.0, linestyle='-', label='1号线')
plt.plot(self.temp_x, self.temp_2[1:], linewidth=1.0, linestyle='-', label='2号线')
plt.plot(self.temp_x, self.temp_3[1:], linewidth=1.0, linestyle='-', label='3号线')
plt.plot(self.temp_x, self.temp_4[1:], linewidth=1.0, linestyle='-', label='4号线')
plt.plot(self.temp_x, self.temp_5[1:], linewidth=1.0, linestyle='-', label='5号线')
plt.plot(self.temp_x, self.temp_6[1:], linewidth=1.0, linestyle='-', label='6号线')
plt.plot(self.temp_x, self.temp_7[1:], linewidth=1.0, linestyle='-', label='7号线')
plt.plot(self.temp_x, self.temp_8[1:], linewidth=1.0, linestyle='-', label='8号线')
plt.plot(self.temp_x, self.temp_9[1:], linewidth=1.0, linestyle='-', label='9号线')
plt.plot(self.temp_x, self.temp_10[1:], linewidth=1.0, linestyle='-', label='10号线')
plt.plot(self.temp_x, self.temp_11[1:], linewidth=1.0, linestyle='-', label='11号线')
plt.plot(self.temp_x, self.temp_12[1:], linewidth=1.0, linestyle='-', label='12号线')
plt.xlabel("采集点")
plt.ylabel("温度")
plt.title('温度采集仪-机器人迈克猫')
plt.legend(loc='upper left')
self.canvas.draw() # 绘制
# 入口函数
if __name__ == "__main__":
app=QApplication(sys.argv)
demo=ZheXianTuOperate()
demo.show()
sys.exit(app.exec_())