代码之家  ›  专栏  ›  技术社区  ›  Edy Bourne

如何更新窗口中显示的matplotlib图表?

  •  0
  • Edy Bourne  · 技术社区  · 5 年前

    我试图写一个每0.5秒更新一次的样本图表。

    代码是这样的:

    import matplotlib.pyplot as plt
    import numpy as np
    import time
    
    # Some example data to display
    x = np.linspace(0, 2 * np.pi, 400)
    y = np.sin(x ** 2)
    
    fig, axs = plt.subplots(2, 2)
    
    i = 5
    for row in x:
        axs[0, 0].plot(x[len(x) - i:], y[len(y) - i:])
        axs[0, 0].set_title("main")
    
        axs[1, 0].plot(x, y ** 2)
        axs[1, 0].set_title(f"shares {i} with main")
        axs[1, 0].sharex(axs[0, 0])
    
        axs[0, 1].plot(x + 1, y + 1)
        axs[0, 1].set_title("unrelated")
    
        axs[1, 1].plot(x + 2, y + 2)
        axs[1, 1].set_title("also unrelated")
    
        fig.canvas.draw()
        fig.canvas.flush_events()
        fig.tight_layout()
        i += 1
        plt.show()
    
        print('THIS NEVER PRINTS - IT SEEMS TO BLOCK ON THE show() CALL ABOVE')
        time.sleep(0.5)
    

    如果运行它,将显示图表。但是 show() 调用似乎被阻塞,循环永远不会执行以更新图表。

    我怎样才能更新它?

    0 回复  |  直到 5 年前