0

I am working on a live plot.I am getting data from a spectrum analyser which gives me the value at a certain frequency. But the program becomes slower the longer it runs. So I hope you have some ideas. I also looked at my activity monitor while running and the RAM isn't full at all.

I tried to comment out ctf = ax.contourf( a, b, B, cmap=cma) which is responsible for plotting and if it don't need to draw it is so fast. But I need the plot so not drawing is not a solution at all.

And ax = plt.subplot( 111, polar = True) for extra information.

Here is my code:

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

Thats how the plot looks like, and with every new step a new piece of the circle is drawn. enter image description here

EDIT

I found following question here on stack overflow: real-time plotting in while loop with matplotlib

I think the answer with 22 Upvotes could be helpful. Has anyone ever used blit ? I have no idea yet how to combine it with my code.

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

Community
  • 1
  • 1
Peter
  • 341
  • 1
  • 4
  • 16
  • blitting lets you update and artist and only redraw _that_ artist which can save a lot of rendering time. – tacaswell Sep 02 '15 at 15:59

1 Answers1

0

I want to answer my own question again.

The best way to optimize the code is to calculate with modulo 2*pi for the radial values.

I changed my code a bit :

  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)

The problem before was that Python also plotted all the old pieces, because obviously the were still there but only under a layer of newly plotted data pieces. So although you didn't see the old plotted data, it was still drawn. Now only the circle from 0 to 2pi is redrawn.

Peter
  • 341
  • 1
  • 4
  • 16