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

后端 未结 4 1631
我在风中等你
我在风中等你 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:35

    This solution example has worked for me on multiple machines. Try adjusting plt.pause(...)

    import matplotlib.pyplot as plt
    import numpy as np
    
    F = lambda x: np.sin(2*x)
    
    plt.ion()    
    x = np.linspace(0, 1, 200)
    plt.plot(x, F(x))
    
    
    for i in range(100):
        if 'ax' in globals(): ax.remove()
        newx = np.random.choice(x, size = 10)
        ax = plt.scatter(newx, F(newx))
        plt.pause(0.05)
    
    plt.ioff()
    plt.show()
    

提交回复
热议问题