Dynamically updating plot in matplotlib

前端 未结 4 615
难免孤独
难免孤独 2020-11-22 08:31

I am making an application in Python which collects data from a serial port and plots a graph of the collected data against arrival time. The time of arrival for the data is

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 09:16

    Here is a way which allows to remove points after a certain number of points plotted:

    import matplotlib.pyplot as plt
    # generate axes object
    ax = plt.axes()
    
    # set limits
    plt.xlim(0,10) 
    plt.ylim(0,10)
    
    for i in range(10):        
         # add something to axes    
         ax.scatter([i], [i]) 
         ax.plot([i], [i+1], 'rx')
    
         # draw the plot
         plt.draw() 
         plt.pause(0.01) #is necessary for the plot to update for some reason
    
         # start removing points if you don't want all shown
         if i>2:
             ax.lines[0].remove()
             ax.collections[0].remove()
    

提交回复
热议问题