I am reading an analog signal in volts (0 - 1 V) using an adafruit ADA1115 chip. I want to save the data with at least 100 sps and plot it real time at the same time (plotting can be done at 1-10 Hz).
My first try regarding the plotting uses the code below (only the main loop)
def Save_Values():
print("Saving...")
plt.ion() # starting figure
fig = plt.figure(figsize=(16,12))
ax = fig.add_subplot(111)
x = np.linspace(0, 1, 1000)
y = np.arange(-200,4000,4.2)
Line1, =ax.plot(x, y, 'r-')
plt.pause(0.01)
fig.canvas.draw()
Last_Values = y
plt.show()
plt.pause(0.01)
Start = time.time()
time_print =0
Print_Interval=.1
adc.start_adc(0, gain=GAIN, data_rate=250)
print('Reading ADS1x15 values, press Ctrl-C to quit...')
# Main loop.
value= 0
while True:
value = adc.get_last_result()
time_measure = time.time() - Start
CSV_Write(time_measure, value)
print(value)
time_aux=time_measure - time_print
if time_aux > Print_Interval:
Last_Values = np.hstack((Last_Values[1:1000],value))
Line1.set_ydata(Last_Values)
fig.canvas.draw()
time_print = time_measure
print("sec")
print(time_measure)
I can set how often the graph is drawn by changing the 'time_print' variable.
The problem is that everytime the graph is redrawn using the command 'fig.canvas.draw()', a couple of milliseconds are lost and some data is not saved, so the data looks like this:
Is there a more efficient function than 'fig.canvas.draw()' or a more efficient python approach altogether?