Python优化图cod

2024-10-03 04:35:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个现场绘图。我从频谱分析仪上得到数据,它给了我某个频率的值。但是程序运行的时间越长,速度就越慢。 所以我希望你有一些想法。我还查看了我的活动监视器,但内存根本没有满。你知道吗

我试着注释掉负责绘图的ctf = ax.contourf( a, b, B, cmap=cma),如果它不需要绘图,那么它的速度就很快。但我需要情节,所以不画根本不是解决办法。你知道吗

ax = plt.subplot( 111, polar = True)获取更多信息。你知道吗

这是我的密码:

while True :
 trace = inst.query(':TRACe:DATA? TRACE1').partition(' ')[2][:-2].split(', ')# the first & last 2 entries are cut off, are random numbers

 for value in trace : #write to file 
    f.write(value)
    f.write('\n')

 try : #looking if data is alright
    trace = np.array(trace, np.float)
 except ValueError: #if a ValueError is raised this message is displayed but the loop won't break and the piece is plotted in one color (green) 
    print'Some wrong data at the', i+1, 'th measurement' 
    longzeroarray = np.zeros(801)

    a = np.linspace(i*np.pi/8-np.pi/16, i*np.pi/8+np.pi/16, 2)#Angle, circle is divided into 16 pieces
    b = np.linspace(start -scaleplot, stop,801) #points of the frequency + 200  more points to gain the inner circle
    A, B = np.meshgrid(a, longzeroarray)
    cma = ListedColormap(['w'])

    #actual plotting
    ctf = ax.contourf( a, b, B, cmap=cma)

    xCooPoint = i*np.pi/8 + np.pi/16 #shows the user the position of the plot
    yCooPoint = stop
    ax.plot(xCooPoint, yCooPoint, 'or', markersize = 15)

    xCooWhitePoint = (i-1) * np.pi/8 + np.pi/16 #this erases the old red points
    yCooWhitePoint = stop
    ax.plot(xCooWhitePoint, yCooWhitePoint, 'ow', markersize = 15)


    plt.draw()
    time.sleep(60) #delaying the time to give analyser time to give us new correct data in the next step 
    i +=1
    continue

 maximasearch(trace,searchrange)

 trace = np.insert(trace,0,zeroarray)
 a = np.linspace(i*np.pi/8+np.pi/16-np.pi/8, i*np.pi/8+np.pi/16, 2)#Angle, circle is divided into 16 pieces
 b = np.linspace(start -scaleplot, stop,801) #points of the frequency + 200  more points to gain the inner circle
 A, B = np.meshgrid(a, trace)

 #actual plotting
 ctf = ax.contourf(a, b, B, cmap=cm.jet,  vmin=-100, vmax=100)

 xCooPoint = i*np.pi/8 + np.pi/16 #shows the user the position of the plot
 yCooPoint = stop
 ax.plot(xCooPoint, yCooPoint, 'or', markersize = 15)

 xCooWhitePoint = (i-1) * np.pi/8 + np.pi/16 #this erases the old red points
 yCooWhitePoint = stop
 ax.plot(xCooWhitePoint, yCooWhitePoint, 'ow', markersize = 15)


 plt.draw()
 i+=1

情节就是这样,每走一步,就会画出一个新的圆。 enter image description here

编辑

我在这里发现了以下关于堆栈溢出的问题:real-time plotting in while loop with matplotlib

我认为22票以上的答案可能会有所帮助。有人用过blit吗?我还不知道如何把它和我的代码结合起来。你知道吗

http://wiki.scipy.org/Cookbook/Matplotlib/Animations


Tags: ofthetoinplotisnppi
1条回答
网友
1楼 · 发布于 2024-10-03 04:35:13

我想再次回答我自己的问题。你知道吗

优化代码的最佳方法是用模2*pi计算径向值。你知道吗

我把代码改了一点:

  a = np.linspace((i*np.pi/8+np.pi/16-np.pi/8)%(np.pi*2), (i*np.pi/8+np.pi/16)%(np.pi*2), 2)

以前的问题是Python也绘制了所有旧的数据块,因为很明显,这些数据块仍然存在,但只在新绘制的数据块层下。所以,尽管你没有看到旧的绘制数据,它仍然被绘制出来。现在只重绘0到2pi之间的圆。你知道吗

相关问题 更多 >