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':
How do I clear both plots?
CodePudding user response:
The problem is that plt.cla()
only works on the current subplot.
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.