用python进行实时温度绘图

时间:2016-09-05 13:54:23

标签: python matplotlib

我目前正在制作一个项目,需要对温度,压力,湿度等各种数量进行实时监控。我正在研究制作所有传感器的单个阵列并使用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()

这是我在互联网上发现的好方法,它正在发挥作用。我面临的唯一问题是,如果我为其他传感器制作更多阵列,并且当我将它与秒表进行比较时制作的情节实际上比较落后,则会消耗更多时间。

是否有任何好的和有效的方法来获得实时图表,这些图表对于大量传感器而言是有效的,并且不会实时滞后。 或者任何清除已经绘制的数组值的命令?

如果有人能帮我解决这个问题,我将不得不承担责任。

1 个答案:

答案 0 :(得分:0)

  

我想问一下,在数组中填充数据是否会继续使进程变慢或者是我的误解?

那个很容易测试;创建一个空列表并将几千个值附加到它需要大约10 ^ -4秒,所以这不应该是一个问题。对我来说有点令人惊讶的是,它实际上比创建和填充固定大小pom.xml更快(但这可能取决于列表/数组的大小)。

我快速使用了您的numpy.ndarray功能,将makeafig()放在(包括)a = fig1.add_subplot(111)一个简单的a.plot(..)循环中,for i in range(1,5);这使a = fig1.add_subplot(2,2,i)慢了大约50%,但差异只有0.1-0.2秒。这与你遇到的滞后一致吗?

关于我可以在没有真实数据的情况下测试的内容,我的下一步是将部分从makeafig()计时到ipcon=..。也许瓶颈只是检索数据?我确定在SO上有几个关于如何计算Python脚本部分时间的例子,对于这类问题,例如下面的例子就足够了:

temperature1=..
相关问题