简体   繁体   English

为什么 plt.cla() 只适用于其中一个地块?

[英]Why does plt.cla() only work on one of the plots?

I am trying to create a program that has two different plots at the same time:我正在尝试创建一个同时具有两个不同图的程序:

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)

However, plt.cla() only seems to work on the second plot. The first plot seems to get 'squished':但是, plt.cla()似乎只适用于第二个 plot。第一个 plot 似乎被“压扁”了: 地块 How do I clear both plots?我如何清除这两个地块?

The problem is that plt.cla() only works on the current subplot.问题是plt.cla()只适用于当前的子图。 To fix the problem you need to run it after each subplot, like:要解决这个问题,您需要在每个子图之后运行它,例如:

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)

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动画中的 plt.CLA 或 CLF - 为什么只显示最新的 plot 对我不起作用? - plt.CLA or CLF in animations - Why does it not work for me to only show most recent plot? 即使在 matplotlib 中的 plt.cla() 之后,如何强制 xlim 永久不变 - How to force xlim unchanged permanently, even after plt.cla() in matplotlib Matplotlib 绘制了两次图,但 plt.plot 只被调用一次? - Matplotlib is plotting plots twice, but plt.plot is only called once? 为什么此列表理解仅适用于一个列表? - Why does this list comprehension only work on one list? 为什么我的if和elif语句一次只能用于一个按钮? - Why does my if and elif statements only work for one button at a time? 如何控制matplotlib中图形周围的空白?(plt.tight_layout不起作用) - How does one control whitespace around a figure in matplotlib?(plt.tight_layout does not work) 功能python - 为什么这些生成器中只有一个需要list()才能工作? - Functional python — why does only one of these generators require list() to work? "Python plt:关闭或清除数字不起作用" - Python plt: close or clear figure does not work 多图只成一个 - Multiple Plots into only one 如何解压缩fig,ax = plt.subplots()是否适用于多个子图? - How does unpacking in fig, ax = plt.subplots() work for more than one subplot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM