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

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

    If you want draw and not freeze your thread as more point are drawn you should use plt.pause() not time.sleep()

    im using the following code to plot a series of xy coordinates.

    import matplotlib.pyplot as plt 
    import math
    
    
    pi = 3.14159
    
    fig, ax = plt.subplots()
    
    x = []
    y = []
    
    def PointsInCircum(r,n=20):
        circle = [(math.cos(2*pi/n*x)*r,math.sin(2*pi/n*x)*r) for x in xrange(0,n+1)]
        return circle
    
    circle_list = PointsInCircum(3, 50)
    
    for t in range(len(circle_list)):
        if t == 0:
            points, = ax.plot(x, y, marker='o', linestyle='--')
            ax.set_xlim(-4, 4) 
            ax.set_ylim(-4, 4) 
        else:
            x_coord, y_coord = circle_list.pop()
            x.append(x_coord)
            y.append(y_coord)
            points.set_data(x, y)
        plt.pause(0.01)
    

提交回复
热议问题