Matplotlib动态展示折线图项目实战(附代码)

本文分享了一个利用matplotlib动态展示折线图的项目,背景是为了解决产品在不同场景下温度监测的问题。作者提到在实际工作中,虽然使用了Fluke设备,但存在功能冗余和二次开发困难的问题,因此决定自行开发一个温度采集仪。文章重点介绍了如何使用matplotlib实现动态折线图的代码实现。
摘要由CSDN通过智能技术生成

序言

这里想跟大家分享的一个技术点是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_())
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器人迈克猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值