0

I have a code which gives me a output. Part of the code is given below. I am trying to plot the output "xin" vs "tstep" in realtime. The code works but it plots xin in a new window each time and its very slow. Please suggest me a way out to plot it faster and plot the data in one plot.

    tstep=1
    fig=plt.figure()
    plt.axis([-300,400,600,0])
    x=list()
    y=list()
    plt.ion()
    plt.show()
    while tstep<tend+1:
        tval=tstep
        phase=0
        if xin<intfxpos[0]+tan(intfang[0])*t*(tstep-1):    
            phase=1
            acount=acount+1
        else:
            bcount=bcount+1
        x.append(xin)
        y.append(tstep-1)
        plt.scatter((xin),(tstep-1))
        #tstep=tend+1
        plt.draw()
        time.sleep(0.05)
        plt.pause(0.0005)

1 Answers1

1

This thread seems to be very similar to this one. The code you have posted seems to be from one of the answers there, and does not open new windows at each draw for me.

You can get closer to real-time plotting by using matplotlib's animation API. Also in that thread is an example of the animation API at work, with very high FPS. You have to add fig.show() just after the line reading fig,ax = subplots(1,1), and then call run() at the very bottom outside of the function definitions.

Community
  • 1
  • 1
tsj
  • 758
  • 3
  • 23