matplotlib.pyplot非阻塞图变得无法响应

时间:2016-06-09 16:21:55

标签: python matplotlib plot nonblocking spectrum

我正在编写一个脚本,它会在预定的时间内连续更新matplotlib图,同时允许我的其余代码在后台运行。下面的代码“有效”并导致绘图根据需要更新约5秒钟,然后绘图窗口变得无响应。

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

我的初步准备是否遵循最佳做法?您可以在绘图中更新/更改数据的次数是否有限制,或者绘图中是否有内存限制?我尝试过使用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()    

0 个答案:

没有答案