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

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

    I know this question is old, but there's now a package available called drawnow on GitHub as "python-drawnow". This provides an interface similar to MATLAB's drawnow -- you can easily update a figure.

    An example for your use case:

    import matplotlib.pyplot as plt
    from drawnow import drawnow
    
    def make_fig():
        plt.scatter(x, y)  # I think you meant this
    
    plt.ion()  # enable interactivity
    fig = plt.figure()  # make a figure
    
    x = list()
    y = list()
    
    for i in range(1000):
        temp_y = np.random.random()
        x.append(i)
        y.append(temp_y)  # or any arbitrary update to your figure's data
        i += 1
        drawnow(make_fig)
    

    python-drawnow is a thin wrapper around plt.draw but provides the ability to confirm (or debug) after figure display.

提交回复
热议问题