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():
     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-')
     Last_Values = y
     Start = time.time()
     time_print =0
     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)
         time_aux=time_measure - time_print
         if time_aux > Print_Interval:
             Last_Values = np.hstack((Last_Values[1:1000],value))
             time_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:

Saved data with printing every second

Is there a more efficient function than 'fig.canvas.draw()' or a more efficient python approach altogether?

1 Answer 1


I don't support Python, so I cannot tell you if there's a more efficient function to call. I can say that you will probably do better if you run the data acquisition and plotting in separate threads or processes rather than inside the same loop. This will prevent your plotting routine from blocking you data acquisition while it works.

You could do this by starting two Python processes performing the two tasks, possibly controlled by a batch script or other program to start them and end them together. If you use a different language, then you'd have better tools for multithreading and only need one process.

  • This is actually a very good option. I already have two scripts, with one of them not logging them just to monitor the signal. Both scripts do not even need to be synchronized. Will experiment with it and report how it goes/accept your answer if it works.
    – Keine
    Commented Aug 29, 2017 at 16:45
  • You may face a race condition on the file I/O if you do it from two scripts, just FYI. If that's the case, then you'll probably need synchronization and buffering of output. In turn, that would likely push you toward something other than python for the main program so that you have better tools for multi-threading. (You might be able to call out to your python from a new main if you want to reuse existing code and structure the program properly.)
    – Brick
    Commented Aug 29, 2017 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.