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

前端 未结 12 1379
天命终不由人
天命终不由人 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 02:01

    An example use-case to plot CPU usage in real-time.

    import time
    import psutil
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    i = 0
    x, y = [], []
    
    while True:
        x.append(i)
        y.append(psutil.cpu_percent())
    
        ax.plot(x, y, color='b')
    
        fig.canvas.draw()
    
        ax.set_xlim(left=max(0, i - 50), right=i + 50)
        fig.show()
        plt.pause(0.05)
        i += 1
    

提交回复
热议问题