Python matplotlib.pyplot非阻塞打印变为无响应

Python matplotlib.pyplot非阻塞打印变为无响应,python,matplotlib,plot,nonblocking,spectrum,Python,Matplotlib,Plot,Nonblocking,Spectrum,我正在编写一个脚本,它将在预定的时间内连续更新matplotlib绘图,同时允许我的其余代码在后台运行。下面的代码“起作用”,使绘图按需更新约5秒,然后绘图窗口变得无响应 我的代码中的第一个块为后续更新准备了绘图图形,而“频谱图”块是更新发生的位置 我的初步准备是否遵循最佳实践?在绘图中更新/更改数据的次数是否有限制,或者绘图中是否有内存限制?我尝试过使用plt.ion(),但它不会导致非阻塞行为 #prepare plot window for periodic updates plt.fig

我正在编写一个脚本,它将在预定的时间内连续更新matplotlib绘图,同时允许我的其余代码在后台运行。下面的代码“起作用”,使绘图按需更新约5秒,然后绘图窗口变得无响应

我的代码中的第一个块为后续更新准备了绘图图形,而“频谱图”块是更新发生的位置

我的初步准备是否遵循最佳实践?在绘图中更新/更改数据的次数是否有限制,或者绘图中是否有内存限制?我尝试过使用plt.ion(),但它不会导致非阻塞行为

#prepare plot window for periodic updates
plt.figure(selection)
plt.subplot(111, axisbg='k')
specPlot,  = plt.plot([], [], 'y')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Amplitude (dBm)')
plt.title('Spectrum')
plt.show(block=False) #required to update plot w/o stopping the script
plt.xlim(np.amin(freq), np.amax(freq))
plt.ylim(refLevel.value-100, refLevel.value)


"""#################ACQUIRE/PROCESS DATA#################"""
#start acquisition
spectrums = 0
rsa.DEVICE_Run()
start = time.clock()
while end - start < acqTime:
    rsa.SPECTRUM_AcquireTrace()
    while ready.value == False:
        rsa.SPECTRUM_WaitForDataReady(timeoutMsec, byref(ready))
    ready.value = False
    rsa.SPECTRUM_GetTrace(c_int(0), specSet.traceLength, 
        byref(traceData), byref(outTracePoints))
    spectrums += 1


    """#################SPECTRUM PLOT#################"""

    #update spectrum trace
    specPlot.set_xdata(freq)
    specPlot.set_ydata(traceData)

    #calculate and annotate peak power and frequency
    peakPower = np.amax(traceData)
    peakPowerFreq = freq[np.argmax(traceData)]
    #print('Peak power in spectrum: %4.3f dBm @ %d Hz' % 
     #   (peakPower, peakPowerFreq))

    peakFreqLine = plt.axvline(x=peakPowerFreq)
    text_x = specSet.actualStartFreq + specSet.span/20
    peakPowerText = plt.text(text_x, peakPower, 
        'Peak power in spectrum: %4.3f dBm @ %5.4f MHz' % 
        (peakPower, peakPowerFreq/1e6), color='white')

    plt.draw()
    peakFreqLine.remove()
    peakPowerText.remove()
    end = time.clock()

#comment this out if you want the plot to stay until the script finishes
rsa.DEVICE_Stop()
plt.close()    
#为定期更新准备绘图窗口
plt.图(选择)
plt.子地块(111,axisbg='k')
specPlot,=plt.plot([]、[]、‘y’)
plt.xlabel(‘频率(Hz)’)
plt.ylabel('振幅(dBm)')
产品名称(“光谱”)
plt.show(block=False)#需要在不停止脚本的情况下更新绘图
plt.xlim(np.amin(频率),np.amax(频率))
plt.ylim(refLevel.value-100,refLevel.value)
“获取/处理数据”
#开始采集
光谱=0
rsa.DEVICE_Run()
开始=时间。时钟()
结束-开始