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

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

    Another option is to go with bokeh. IMO, it is a good alternative at least for real-time plots. Here is a bokeh version of the code in the question:

    from bokeh.plotting import curdoc, figure
    import random
    import time
    
    def update():
        global i
        temp_y = random.random()
        r.data_source.stream({'x': [i], 'y': [temp_y]})
        i += 1
    
    i = 0
    p = figure()
    r = p.circle([], [])
    curdoc().add_root(p)
    curdoc().add_periodic_callback(update, 100)
    

    and for running it:

    pip3 install bokeh
    bokeh serve --show test.py
    

    bokeh shows the result in a web browser via websocket communications. It is especially useful when data is generated by remote headless server processes.

提交回复
热议问题