我下面有这段代码,它在linux上运行得很好,但在mac上不起作用。我试着找出为什么它不起作用,如果我不在onPositionChange
函数上使用matplotlib进行绘图,代码就在mac上起作用。所以我相信问题来自matplotlib
,但我不明白为什么。。。
我在两个系统中都安装了完全相同的matplotlib
库:3.1.2
,我正在使用python3
。
这是我在控制台中的错误:
2021-05-12 16:52:25.941 Python[60747:697875] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff206646af __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff2039c3c9 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2068ca9a -[NSException raise] + 9
3 AppKit 0x00007fff22e33460 -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 321
4 AppKit 0x00007fff22e1e121 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1375
5 AppKit 0x00007fff22e1dbbb -[NSWindow initWithContentRect:styleMask:backing:defer:] + 42
6 _macosx.cpython-39-darwin.so 0x0000000125d10755 -[Window initWithContentRect:styleMask:backing:defer:withManager:] + 69
7 _macosx.cpython-39-darwin.so 0x0000000125d13abf FigureManager_init + 287
8 Python 0x000000010ac94d56 wrap_init + 12
9 Python 0x000000010ac2fc93 wrapperdescr_call + 411
10 Python 0x000000010ac256da _PyObject_MakeTpCall + 129
11 Python 0x000000010ad1dcbd call_function + 270
12 Python 0x000000010ad1b526 _PyEval_EvalFrameDefault + 45324
13 Python 0x000000010ac26240 _PyFunction_Vectorcall + 192
14 Python 0x000000010ac25907 _PyObject_FastCallDictTstate + 87
15 Python 0x000000010ac94aea slot_tp_init + 192
16 Python 0x000000010ac9e2cc type_call + 272
17 Python 0x000000010ac256da _PyObject_MakeTpCall + 129
18 Python 0x000000010ad1dcbd call_function + 270
19 Python 0x000000010ad1b526 _PyEval_EvalFrameDefault + 45324
20 Python 0x000000010ac26240 _PyFunction_Vectorcall + 192
21 Python 0x000000010ac28e7a method_vectorcall + 160
22 Python 0x000000010ad1dc53 call_function + 164
23 Python 0x000000010ad1b526 _PyEval_EvalFrameDefault + 45324
24 Python 0x000000010ad0f2d6 _PyEval_EvalCode + 403
25 Python 0x000000010ac262f8 _PyFunction_Vectorcall + 376
26 Python 0x000000010ac28e7a method_vectorcall + 160
27 Python 0x000000010ac25ee1 PyVectorcall_Call + 164
28 Python 0x000000010ad1b75e _PyEval_EvalFrameDefault + 45892
29 Python 0x000000010ad0f2d6 _PyEval_EvalCode + 403
30 Python 0x000000010ac262f8 _PyFunction_Vectorcall + 376
31 Python 0x000000010ad1dc53 call_function + 164
32 Python 0x000000010ad1b562 _PyEval_EvalFrameDefault + 45384
33 Python 0x000000010ac26240 _PyFunction_Vectorcall + 192
34 Python 0x000000010ad1dc53 call_function + 164
35 Python 0x000000010ad1b562 _PyEval_EvalFrameDefault + 45384
36 Python 0x000000010ad0f2d6 _PyEval_EvalCode + 403
37 Python 0x000000010ac262f8 _PyFunction_Vectorcall + 376
38 Python 0x000000010ad1dc53 call_function + 164
39 Python 0x000000010ad1b562 _PyEval_EvalFrameDefault + 45384
40 Python 0x000000010ad0f2d6 _PyEval_EvalCode + 403
41 Python 0x000000010ac262f8 _PyFunction_Vectorcall + 376
42 Python 0x000000010ad1dc53 call_function + 164
43 Python 0x000000010ad1b612 _PyEval_EvalFrameDefault + 45560
44 Python 0x000000010ac26240 _PyFunction_Vectorcall + 192
45 Python 0x000000010ad1dc53 call_function + 164
46 Python 0x000000010ad1b526 _PyEval_EvalFrameDefault + 45324
47 Python 0x000000010ac26240 _PyFunction_Vectorcall + 192
48 Python 0x000000010ac28f47 method_vectorcall + 365
49 _ctypes.cpython-39-darwin.so 0x000000010b23eb28 _CallPythonObject + 568
50 _ctypes.cpython-39-darwin.so 0x000000010b23e8ea closure_fcn + 46
51 libffi.dylib 0x00007fff2db304bb ffi_closure_unix64_inner + 481
52 libffi.dylib 0x00007fff2db30a7c ffi_closure_unix64 + 72
53 Phidget22 0x0000000126155b2f PhidgetChannel_bridgeInput + 907
54 Phidget22 0x000000012619b120 PhidgetDispatcher + 1305
55 libsystem_pthread.dylib 0x00007fff204f2950 _pthread_start + 224
56 libsystem_pthread.dylib 0x00007fff204ee47b thread_start + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from Phidget22.Phidget import *
from Phidget22.Devices.Encoder import *
import time
import json
import numpy as np
from scipy import zeros, signal, random
import matplotlib.pyplot as plt
from scipy import signal
from numpy import linalg as LA
array = []
bigArray = []
bigArrayControl = []
filterData = []
i = 0
ii = 0
dataInterval = 20
index = 0
def filter_sbs(data):
data = random.random(2000)
b = signal.firwin(50, 0.05)
z = signal.lfilter_zi(b, 1)
result = zeros(data.size)
result[i], z = signal.lfilter(b, 1, data, axis=0, zi=z)
return result
def butter(data):
fs = 1000 / dataInterval # Sampling frequency
L = len(data)
print(L)
# # Generate the time vector properly
t = np.arange(L) / fs
fc = 20 # Cut-off frequency of the filter
w = fc / (fs / 2) # Normalize the frequency
b, a = signal.butter(4, w, 'low')
outputX = signal.filtfilt(b, a, data,axis=0)
return outputX
def filterRealTime(data):
global zi, ii
fs = 1000 / dataInterval # Sampling frequency
L = len(data)
t = np.arange(L) / fs
fc = 20 # Cut-off frequency of the filter
w = fc / (fs / 2) # Normalize the frequency
b, a = signal.butter(4, w, 'low')
if (ii == 0):
ii = 1
zi = signal.lfiltic(b,a,[0])
y, zi = signal.lfilter(b,a, data, axis=0, zi=zi)
#print(zi)
return y
def onPositionChange(self, positionChange, timeChange, indexTriggered):
global i
global array, bigArray, filterData
array.append(positionChange)
bigArray.append(positionChange)
send_msg = {
'data': array,
}
if (len(array) == 50 and len(bigArray) <= 500):
global index, filterData, bigArrayControl
result = filterRealTime(array)
bigArrayControl.extend(array)
array = []
index = index + 1
filterData.extend(result)
if (len(bigArray) == 50):
npBigArray = np.array(bigArray)
data = random.random(50)
result = butter(npBigArray)
print(filterData)
plt.plot(filterData, 'b-', label='filter')
plt.show()
def main():
encoder0 = Encoder()
encoder0.setOnPositionChangeHandler(onPositionChange)
encoder0.openWaitForAttachment(5000)
encoder0.setDataInterval(dataInterval)
try:
input("Press Enter to Stopn")
except (Exception, KeyboardInterrupt):
pass
encoder0.close()
main()
您是否尝试将Matplotlib的导入更改为此?
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
或
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib import pyplot as plt