I am attempting to implement primitive live graphing with matplotlib. The script below is supposed to watch to see if a file changes (which is being updated live by another service), and then update a matplotlib figure based on the data in the file. This figure needs to be non blocking, so that the loop can continue to execute and I can have the 'live' feel. I came up with the script below which achieves what I am looking for, however after a few seconds with no data coming in, I get the message 'Matplotlib is not responding'. I understand this is because it is stuck in the loop, and the system thinks it is hanging on a task, but how can I avoid this error?
I have also tried moving the plt.show outside the if statement, and the steps in Plotting in a non-blocking way with Matplotlib for an alternate non-blocking method, but both yielded similar results.
import os
import matplotlib.pyplot as plt
import time
_cached_stamp = -1
while True:
stamp = os.stat(filename).st_mtime #check if the file has been modified
if stamp != _cached_stamp:
_cached_stamp = stamp
#... (retrieve x and y from the file)
plt.plot(x, y)
plt.pause(0.1)
plt.show(block=False)
time.sleep(0.1)
Additionally, the window pushes itself to the front of the screen whenever receiving an update...is it be possible in matplotlib to keep this window in the background when receiving updates? Or should I switch to an alternative graphing library for either of these issues?