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

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

    Here's the working version of the code in question (requires at least version Matplotlib 1.1.0 from 2011-11-14):

    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.axis([0, 10, 0, 1])
    
    for i in range(10):
        y = np.random.random()
        plt.scatter(i, y)
        plt.pause(0.05)
    
    plt.show()
    

    Note some of the changes:

    1. Call plt.pause(0.05) to both draw the new data and it runs the GUI's event loop (allowing for mouse interaction).

提交回复
热议问题