1
votes

I am trying to write python code that continuously gets data from machine and plots these data in a figure window. I am using matplotlib with interactive plotting enabled / ion().

There is quite a bit of data, so plotting may take a while. Since the python code does not continue until the plot is updated, data acquisition is stopped while the plot updated.

I'd like to avoid the data gaps resulting from the updating of the plots. Is there a (simple and reliable) way to update the figure/plot without blocking the execution of the code until the plot is updated on screen?

EDIT 23.9.2015:

I tried threading as suggested below. I put this in a script:

import threading
import matplotlib.pyplot as plt

def plotter():
    print 'Starting plot...'
    plt.plot([1,2,3,4])
    plt.show()
    print '...plot done.'
    return

t = threading.Thread(target=plotter)
t.start()

Executing this script results in a crash (Mac OS X with Python 2.7 from MacPorts; see below). The script works smoothly and as expected if I comment out plt.plot(...) and plt.show(). Any help or suggestions what's wrong?

$ python plot_in_own_thread.py 
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')
Starting plot...
2016-09-23 08:43:19.433 Python[89176:39798237] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1259/Misc.subproj/NSUndoManager.m:359
2016-09-23 08:43:19.433 Python[89176:39798237] +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.
2016-09-23 08:43:19.435 Python[89176:39798237] (
    0   CoreFoundation                      0x00007fff8cbb14f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff9f7ab73c objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff8cbb61ca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff9ba2d856 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   Foundation                          0x00007fff9b9b2af1 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
    5   AppKit                              0x00007fff98061e22 -[NSApplication run] + 844
    6   _macosx.so                          0x000000010d3494a2 show + 210
    7   Python                              0x000000010aff2539 PyEval_EvalFrameEx + 27929
    8   Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    9   Python                              0x000000010aff6e36 fast_function + 118
    10  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    11  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    12  Python                              0x000000010af771cc function_call + 364
    13  Python                              0x000000010af514c3 PyObject_Call + 99
    14  Python                              0x000000010af5e526 instancemethod_call + 182
    15  Python                              0x000000010af514c3 PyObject_Call + 99
    16  Python                              0x000000010afac5fb slot_tp_call + 171
    17  Python                              0x000000010af514c3 PyObject_Call + 99
    18  Python                              0x000000010aff2c8c PyEval_EvalFrameEx + 29804
    19  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    20  Python                              0x000000010aff6e36 fast_function + 118
    21  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    22  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    23  Python                              0x000000010af771cc function_call + 364
    24  Python                              0x000000010af514c3 PyObject_Call + 99
    25  Python                              0x000000010aff2c8c PyEval_EvalFrameEx + 29804
    26  Python                              0x000000010aff6f16 fast_function + 342
    27  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    28  Python                              0x000000010aff6f16 fast_function + 342
    29  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    30  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    31  Python                              0x000000010af771cc function_call + 364
    32  Python                              0x000000010af514c3 PyObject_Call + 99
    33  Python                              0x000000010af5e526 instancemethod_call + 182
    34  Python                              0x000000010af514c3 PyObject_Call + 99
    35  Python                              0x000000010aff68b5 PyEval_CallObjectWithKeywords + 165
    36  Python                              0x000000010b030cb6 t_bootstrap + 70
    37  libsystem_pthread.dylib             0x00007fff9b92b99d _pthread_body + 131
    38  libsystem_pthread.dylib             0x00007fff9b92b91a _pthread_body + 0
    39  libsystem_pthread.dylib             0x00007fff9b929351 thread_start + 13
)
2016-09-23 08:43:19.436 Python[89176:39798237] *** Assertion failure in +[NSUndoManager _endTopLevelGroupings], /Library/Caches/com.apple.xbs/Sources/Foundation/Foundation-1259/Misc.subproj/NSUndoManager.m:359
2016-09-23 08:43:19.438 Python[89176:39798237] An uncaught exception was raised
2016-09-23 08:43:19.438 Python[89176:39798237] +[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.
2016-09-23 08:43:19.438 Python[89176:39798237] (
    0   CoreFoundation                      0x00007fff8cbb14f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff9f7ab73c objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff8cbb61ca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff9ba2d856 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   Foundation                          0x00007fff9b9b2af1 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
    5   AppKit                              0x00007fff98061ebe -[NSApplication run] + 1000
    6   _macosx.so                          0x000000010d3494a2 show + 210
    7   Python                              0x000000010aff2539 PyEval_EvalFrameEx + 27929
    8   Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    9   Python                              0x000000010aff6e36 fast_function + 118
    10  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    11  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    12  Python                              0x000000010af771cc function_call + 364
    13  Python                              0x000000010af514c3 PyObject_Call + 99
    14  Python                              0x000000010af5e526 instancemethod_call + 182
    15  Python                              0x000000010af514c3 PyObject_Call + 99
    16  Python                              0x000000010afac5fb slot_tp_call + 171
    17  Python                              0x000000010af514c3 PyObject_Call + 99
    18  Python                              0x000000010aff2c8c PyEval_EvalFrameEx + 29804
    19  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    20  Python                              0x000000010aff6e36 fast_function + 118
    21  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    22  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    23  Python                              0x000000010af771cc function_call + 364
    24  Python                              0x000000010af514c3 PyObject_Call + 99
    25  Python                              0x000000010aff2c8c PyEval_EvalFrameEx + 29804
    26  Python                              0x000000010aff6f16 fast_function + 342
    27  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    28  Python                              0x000000010aff6f16 fast_function + 342
    29  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    30  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    31  Python                              0x000000010af771cc function_call + 364
    32  Python                              0x000000010af514c3 PyObject_Call + 99
    33  Python                              0x000000010af5e526 instancemethod_call + 182
    34  Python                              0x000000010af514c3 PyObject_Call + 99
    35  Python                              0x000000010aff68b5 PyEval_CallObjectWithKeywords + 165
    36  Python                              0x000000010b030cb6 t_bootstrap + 70
    37  libsystem_pthread.dylib             0x00007fff9b92b99d _pthread_body + 131
    38  libsystem_pthread.dylib             0x00007fff9b92b91a _pthread_body + 0
    39  libsystem_pthread.dylib             0x00007fff9b929351 thread_start + 13
)
2016-09-23 08:43:19.438 Python[89176:39798237] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+[NSUndoManager(NSInternal) _endTopLevelGroupings] is only safe to invoke on the main thread.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff8cbb14f2 __exceptionPreprocess + 178
    1   libobjc.A.dylib                     0x00007fff9f7ab73c objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff8cbb61ca +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00007fff9ba2d856 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
    4   Foundation                          0x00007fff9b9b2af1 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
    5   AppKit                              0x00007fff98061ebe -[NSApplication run] + 1000
    6   _macosx.so                          0x000000010d3494a2 show + 210
    7   Python                              0x000000010aff2539 PyEval_EvalFrameEx + 27929
    8   Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    9   Python                              0x000000010aff6e36 fast_function + 118
    10  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    11  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    12  Python                              0x000000010af771cc function_call + 364
    13  Python                              0x000000010af514c3 PyObject_Call + 99
    14  Python                              0x000000010af5e526 instancemethod_call + 182
    15  Python                              0x000000010af514c3 PyObject_Call + 99
    16  Python                              0x000000010afac5fb slot_tp_call + 171
    17  Python                              0x000000010af514c3 PyObject_Call + 99
    18  Python                              0x000000010aff2c8c PyEval_EvalFrameEx + 29804
    19  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    20  Python                              0x000000010aff6e36 fast_function + 118
    21  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    22  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    23  Python                              0x000000010af771cc function_call + 364
    24  Python                              0x000000010af514c3 PyObject_Call + 99
    25  Python                              0x000000010aff2c8c PyEval_EvalFrameEx + 29804
    26  Python                              0x000000010aff6f16 fast_function + 342
    27  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    28  Python                              0x000000010aff6f16 fast_function + 342
    29  Python                              0x000000010aff23b8 PyEval_EvalFrameEx + 27544
    30  Python                              0x000000010afeb52a PyEval_EvalCodeEx + 1690
    31  Python                              0x000000010af771cc function_call + 364
    32  Python                              0x000000010af514c3 PyObject_Call + 99
    33  Python                              0x000000010af5e526 instancemethod_call + 182
    34  Python                              0x000000010af514c3 PyObject_Call + 99
    35  Python                              0x000000010aff68b5 PyEval_CallObjectWithKeywords + 165
    36  Python                              0x000000010b030cb6 t_bootstrap + 70
    37  libsystem_pthread.dylib             0x00007fff9b92b99d _pthread_body + 131
    38  libsystem_pthread.dylib             0x00007fff9b92b91a _pthread_body + 0
    39  libsystem_pthread.dylib             0x00007fff9b929351 thread_start + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
1

1 Answers

0
votes

You can use threading which will make it to run independently, see https://docs.python.org/3/library/threading.html for more details