在下面的代码中,我有两个单独的动画,并将它们绘制在两个单独的子图中。我希望它们都在一个单独的图中运行,而不是现在这样。我尝试了下面解释的方法,但是它会导致问题,请求帮助。
我试图创建一个单独的子图,而不是
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time as t
x = np.linspace(0,5,100)
fig = plt.figure()
p1 = fig.add_subplot(2,1,1)
p2 = fig.add_subplot(2,1,2)
def gen1():
i = 0.5
while(True):
yield i
i += 0.1
def gen2():
j = 0
while(True):
yield j
j += 1
def run1(c):
p1.clear()
p1.set_xlim([0,15])
p1.set_ylim([0,100])
y = c*x
p1.plot(x,y,'b')
def run2(c):
p2.clear()
p2.set_xlim([0,15])
p2.set_ylim([0,100])
y = c*x
p2.plot(x,y,'r')
ani1 = animation.FuncAnimation(fig,run1,gen1,interval=1)
ani2 = animation.FuncAnimation(fig,run2,gen2,interval=1)
fig.show()
我试图创建一个单独的子图,而不是
p1
和 p2
,并在该单个子图中绘制这两个图。也就是只绘制一个图而不是两个。据我所知,这是因为其中一个在绘制后立即被清除了。如何解决这个问题呢?