[英]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.