Update Lines in matplotlib

前端 未结 2 368
情歌与酒
情歌与酒 2021-01-14 10:36

I have a graph with multiple data sets on it. I need to continually redraw these lines, each separately, as the data is updated. How can I delete and reestablish it repeated

2条回答
  •  一整个雨季
    2021-01-14 10:52

    #!/usr/bin/env python
    
    import time
    from pylab import *
    
    ion() # turn interactive mode on
    
    # initial data
    x = arange(-8, 8, 0.1);
    y1 = sin(x)
    y2 = cos(x)
    
    # initial plot
    line1, line2, = plot(x, y1, 'r', x, y2, 'b')
    line1.axes.set_xlim(-10, 10)
    line1.axes.set_ylim(-2, 2)
    line1.set_label("line1")
    line2.set_label("line2")
    legend()
    grid()
    draw()
    
    # update line 1
    for i in xrange(50):
        time.sleep(0.1)
    
        # update data
        y1 = sin(x + float(i) / 10)
    
        # update plot
        line1.set_ydata(y1)
        draw()
    
    # update line 2
    for i in xrange(50):
        time.sleep(0.1)
    
        # update data
        y2 = cos(x + float(i) / 10)
    
        # update plot
        line2.set_ydata(y2)
        draw()
    

提交回复
热议问题