2

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.

0 Answers0