我正在制作一个实况图。我从频谱分析仪获取数据,它给出了特定频率的值。但程序运行的时间越长,程序越慢。 所以我希望你有一些想法。我还在运行时查看了我的活动监视器并且RAM根本不满。
我试图评论负责绘图的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
修改
我在堆栈溢出时发现了以下问题:real-time plotting in while loop with matplotlib
我认为22个Upvotes的答案可能会有所帮助。有没有人用过blit
?我还不知道如何将它与我的代码结合起来。
答案 0 :(得分:0)
我想再次回答我自己的问题。
优化代码的最佳方法是使用 modulo 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的圆圈。