【问题标题】:Why does plt.cla() only work on one of the plots?为什么 plt.cla() 只适用于其中一个地块?
【发布时间】:2023-01-29 21:59:05
【问题描述】:

我正在尝试创建一个同时具有两个不同图的程序:

import matplotlib.pyplot as plt
import numpy as np

plt.ion()


for i in range(100):
    x = np.arange(i, i + 50, 0.2)

    plt.cla()

    for subplotId in range(1, 3):
        plt.subplot(2, 1, subplotId)
        plt.ylim(-100, 100)

        y = np.tan(x)
        plt.plot(x, y)

    plt.pause(0.1)

但是,plt.cla() 似乎只适用于第二个情节。 第一个情节似乎被“压扁”了: 我如何清除这两个地块?

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    问题是 plt.cla() 只适用于当前的子图。 要解决这个问题,您需要在每个子图之后运行它,例如:

    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.ion()
    
    
    for i in range(100):
        x = np.arange(i, i + 50, 0.2)
        #don't put plt.cla() here
    
        for subplotId in range(1, 3):
            plt.subplot(2, 1, subplotId)
            plt.cla() # put it here so that it runs for each subplot
            plt.ylim(-100, 100)
    
            y = np.tan(x)
            plt.plot(x, y)
    
        plt.pause(0.1)
    

    我希望这有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2017-03-08
      • 1970-01-01
      • 2014-11-29
      • 2016-01-09
      相关资源
      最近更新 更多