实时绘图以使用绘图监视数据

发布于 2025-01-25 09:44:29 字数 2730 浏览 4 评论 0原文

我正在尝试通过在测试室中丢弃的一组传感器实时获取数据。我们使用matplotlib成功地做到了这一点,但是最好使用绘图,因为这些图可以在其他环境中导出和可视化(考虑到我们的最终目标)。我看到了一些示例 and “ https://dash.plotly.com/live-updates” rel =“ nofollow noreferrer”>在这里,但它不符合我们所拥有的,因为传感器数据采集,数据列表正在不断更新,不通过等式(取决于时间步长集)。传感器由NIDAQMX软件包(国家仪器传感器)读取 - 但显然没有问题。这是我们用Matplotlib绘制的工作代码(传感器的名称存储在列表中,而不是在此处描述):

import nidaqmx
from nidaqmx.constantsimport(TerminalConfiguration,VoltageUnits,ThermocoupleType,CJCSource,TemperatureUnits,ResistanceConfiguration,ExcitationSource,RTDType)
from datetime import datetime,date
import time
import numpy as np
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import matplotlib.animation as animation

sensor_name = "sensor_1"
list_voltage_rse = [...]

date_time = []
data = []
tempo = []
t_min = 2
t_max = 40
time_step = 10
N = 100
x_len = N
k = 1
i = 0
y_range = [t_min, t_max]


#read data from DAQ (specified sensor)

def readdaq():

    with nidaqmx.Task() as task:
       first_inicio = datetime.now()

       if sensor_name in list_voltage_rse:
        task.ai_channels.add_ai_voltage_chan(sensor_name,terminal_config=TerminalConfiguration.RSE,units=VoltageUnits.VOLTS)

       first_fim = datetime.now()
       tempo_leitura = (first_fim - first_inicio).total_seconds()

       task.start()

       value = task.read()

       print(value)

       task.stop()

#Write Data Function

def writefiledata(t, x):
    # Open File
    file = open("tempdata.txt", "a")
    # Write Data
    time = str(t)
    value = str(round(x, 2))
    file.write(time + "\t" + value)
    file.write("\n")
    # Close File
    file.close()

#Create figure for plotting

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = list(range(0, N))
ys = [0] * x_len
ax.set_ylim(y_range)

#Create a blank line. We will update the line in animate

line, = ax.plot(xs, ys)

#Configure Plot

plt.title('Temperature')
plt.xlabel('t [s]')
plt.ylabel('Temp [degC]')
plt.grid()

#Logging Temperature Data from DAQ Device

def logging(i, ys):
    inicio = datetime.now()
    value = readdaq()
    print("T =", round(value,1), "[degC]")
    data.append(value)

final = datetime.now()
tempo_leitura = (final - inicio).total_seconds()
print(tempo_leitura)

time.sleep(time_step - tempo_leitura)
global k
k = k + 1
writefiledata(k*time_step, value)
# Add y to list
ys.append(value)
# Limit y list to set number of items
ys = ys[-x_len:]
# Update line with new Y values
line.set_ydata(ys)
return line,

ani = animation.FuncAnimation(fig,logging,fargs=(ys,),interval=100,blit=True)
plt.show()

我希望此代码能够帮助您了解我想要的内容。

I'm trying to plot data that is acquire in real-time by a set of sensors disposed in a test room. We succeed doing that using matplotlib, however it will be better to use plotly, since the graphs could be exported and visualized in other environments (considering our final goals). I saw some examples here and here but it does not fit what we have, because the data list is being constantly updated by the sensors data acquisition, not by a equation (depending on the time step set). The sensors are read by the nidaqmx package (National Instruments sensors) - but no problems with this part apparently. Here it is the working code that we used to plot using matplotlib (the sensors' name are stored in lists, not described here):

import nidaqmx
from nidaqmx.constantsimport(TerminalConfiguration,VoltageUnits,ThermocoupleType,CJCSource,TemperatureUnits,ResistanceConfiguration,ExcitationSource,RTDType)
from datetime import datetime,date
import time
import numpy as np
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import matplotlib.animation as animation

sensor_name = "sensor_1"
list_voltage_rse = [...]

date_time = []
data = []
tempo = []
t_min = 2
t_max = 40
time_step = 10
N = 100
x_len = N
k = 1
i = 0
y_range = [t_min, t_max]


#read data from DAQ (specified sensor)

def readdaq():

    with nidaqmx.Task() as task:
       first_inicio = datetime.now()

       if sensor_name in list_voltage_rse:
        task.ai_channels.add_ai_voltage_chan(sensor_name,terminal_config=TerminalConfiguration.RSE,units=VoltageUnits.VOLTS)

       first_fim = datetime.now()
       tempo_leitura = (first_fim - first_inicio).total_seconds()

       task.start()

       value = task.read()

       print(value)

       task.stop()

#Write Data Function

def writefiledata(t, x):
    # Open File
    file = open("tempdata.txt", "a")
    # Write Data
    time = str(t)
    value = str(round(x, 2))
    file.write(time + "\t" + value)
    file.write("\n")
    # Close File
    file.close()

#Create figure for plotting

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = list(range(0, N))
ys = [0] * x_len
ax.set_ylim(y_range)

#Create a blank line. We will update the line in animate

line, = ax.plot(xs, ys)

#Configure Plot

plt.title('Temperature')
plt.xlabel('t [s]')
plt.ylabel('Temp [degC]')
plt.grid()

#Logging Temperature Data from DAQ Device

def logging(i, ys):
    inicio = datetime.now()
    value = readdaq()
    print("T =", round(value,1), "[degC]")
    data.append(value)

final = datetime.now()
tempo_leitura = (final - inicio).total_seconds()
print(tempo_leitura)

time.sleep(time_step - tempo_leitura)
global k
k = k + 1
writefiledata(k*time_step, value)
# Add y to list
ys.append(value)
# Limit y list to set number of items
ys = ys[-x_len:]
# Update line with new Y values
line.set_ydata(ys)
return line,

ani = animation.FuncAnimation(fig,logging,fargs=(ys,),interval=100,blit=True)
plt.show()

I hope this code helped to give an idea of what I'm looking for.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文