How do I plot in real-time in a while loop using matplotlib?

前端 未结 12 1359
天命终不由人
天命终不由人 2020-11-22 01:08

I am trying to plot some data from a camera in real time using OpenCV. However, the real-time plotting (using matplotlib) doesn\'t seem to be working.

I\'ve isolated

12条回答
  •  野的像风
    2020-11-22 01:56

    None of the methods worked for me. But I have found this Real time matplotlib plot is not working while still in a loop

    All you need is to add

    plt.pause(0.0001)
    

    and then you could see the new plots.

    So your code should look like this, and it will work

    import matplotlib.pyplot as plt
    import numpy as np
    plt.ion() ## Note this correction
    fig=plt.figure()
    plt.axis([0,1000,0,1])
    
    i=0
    x=list()
    y=list()
    
    while i <1000:
        temp_y=np.random.random();
        x.append(i);
        y.append(temp_y);
        plt.scatter(i,temp_y);
        i+=1;
        plt.show()
        plt.pause(0.0001) #Note this correction
    

提交回复
热议问题