MatPlotLib's ion() and draw() not working

后端 未结 4 1628
我在风中等你
我在风中等你 2021-01-18 08:00

I am trying to plot figures in real time using a for loop. I have the following simple code:

import matplotlib.pyplot as plt

plt.ion()
plt.figure()
for i in         


        
4条回答
  •  逝去的感伤
    2021-01-18 08:29

    Adapted for your case from : Python realtime plotting

    import matplotlib.pyplot as plt
    import numpy as np
    import time
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    # some X and Y data
    x = [0]
    y = [0]
    
    li, = ax.plot(x, y,'o')
    
    # draw and show it
    fig.canvas.draw()
    plt.show(block=False)
    
    # loop to update the data
    for i in range(100):
        try:
            x.append(i)
            y.append(i)
    
            # set the new data
            li.set_xdata(x)
            li.set_ydata(y)
    
            ax.relim() 
            ax.autoscale_view(True,True,True) 
    
            fig.canvas.draw()
    
            time.sleep(0.01)
        except KeyboardInterrupt:
            plt.close('all')
            break
    

提交回复
热议问题