简体   繁体   English

使用python实时绘制温度

[英]Real Time temperature plotting with python

I am currently making a project which requires real time monitoring of various quantities like temperature, pressure, humidity etc. I am following a approach of making individual arrays of all the sensors and ploting a graph using matplotlib and drwnow. 我目前正在做一个项目,需要实时监视各种量,例如温度,压力,湿度等。我正在采用一种方法,对所有传感器进行单独排列,并使用matplotlib和drwnow绘制图形。

HOST = "localhost"
PORT = 4223

UID1 = "tsJ" # S1

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_ptc import BrickletPTC
import numpy as np
import serial

import matplotlib
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')

from drawnow import *

# creating arrays to feed the data

tempC1 = []

def makeafig():

    # creating subplots
    fig1 = plt.figure(1)

    a = fig1.add_subplot(111)

    #setting up axis label, auto formating of axis and title
    a.set_xlabel('Time [s]', fontsize = 10)
    a.set_ylabel('Temperature [°C]', fontsize = 10)
    y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False)
    a.yaxis.set_major_formatter(y_formatter)
    title1 = "Current Room Temperature (Side1): " + str(temperature1/100) + " °C"
    a.set_title(title1, fontsize = 10)

    #plotting the graph
    a.plot(tempC1, "#00A3E0")

    #saving the figure
    fig1.savefig('RoomTemperature.png', dpi=100)

while True:

    ipcon = IPConnection() # Create IP connection
    ptc1 = BrickletPTC(UID1, ipcon) # S1 

    ipcon.connect(HOST, PORT) # Connect to brickd

    #setting the temperature from PTC bricklet

    temperature1 = ptc1.get_temperature()


    #processing data from a temperature sensor to 1st array
    dataArray1=str(temperature1/100).split(',')
    temp1 = float(dataArray1[0])
    tempC1.append(temp1)

    #making a live figure
    drawnow(makeafig)
    plt.draw()

This is the approach I found good on the internet and it is working. 这是我在互联网上发现的不错的方法,并且有效。 The only problem I am facing is It consumes more time if I made more arrays for other sensors and the plot being made lags from the real time when I compare it with a stopwatch. 我面临的唯一问题是,如果我为其他传感器制造更多的阵列,则会消耗更多的时间,并且与秒表进行比较时,绘制的图会比实时滞后。

Is there any good and efficient approach for obtaining live graphs that will be efficient with lot of sensors and doen't lag with real time. 是否有任何良好且有效的方法来获取实时图,这种实时图在使用大量传感器的情况下将是有效的,并且不会因实时而滞后。 Or any command to clear up the already plotted array values? 还是任何命令来清除已经绘制的数组值?

I'd be obliged if anyone can help me with this problem. 如果有人可以帮助我解决这个问题,我将负责。

I'd like to ask that, does filling data in arrays continuosly makes the process slow or is it my misconception? 我想问一下,是否连续地将数据填充到数组中会使过程变慢,还是我的误解?

That one is easy to test; 这个很容易测试; creating an empty list and appending a few thousand values to it takes roughly 10^-4 seconds, so that shouldn't be a problem. 创建一个空列表并向其添加几千个值大约需要10 ^ -4秒,因此这不成问题。 For me somewhat surprisingly, it is actually faster than creating and filling a fixed size numpy.ndarray (but that is probably going to depend on the size of the list/array). 对于我而言,有些令人惊讶的是,它实际上比创建和填充固定大小的numpy.ndarray (但这可能取决于列表/数组的大小)。

I quickly played around with your makeafig() function, placing a = fig1.add_subplot(111) up to (including) a.plot(..) in a simple for i in range(1,5) loop, with a = fig1.add_subplot(2,2,i) ; 我很快就使用了makeafig()函数,将a = fig1.add_subplot(111) a.plot(..)放到一个简单的for i in range(1,5)循环中,直到(包括)一个a.plot(..) ,其中a = fig1.add_subplot(2,2,i) ; that makes makeafig() about 50% slower, but differences are only about 0.1-0.2 seconds. 这会使makeafig()速度降低约50%,但差异仅约为0.1-0.2秒。 Is that in line with the lag that you are experiencing? 这是否与您遇到的滞后相符?

That's about what I can test without the real data, my next step would be to time the part from ipcon=.. to temperature1=.. . 那就是我可以在没有真实数据的情况下测试的内容,下一步是将零件的时间从ipcon=..temperature1=.. Perhaps the bottleneck is simply the retrieval of the data? 瓶颈可能仅仅是数据的检索? I'm sure that there are several examples on SO on how to time parts of Python scripts, for these kind of problems something like the example below should be sufficient: 我确信在SO上有几个关于如何计时Python脚本部分的示例,对于这些问题,像下面的示例这样的东西就足够了:

import time
t0 = time.time()
# do something
dt = time.time() - t0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM