Basically, you can use a very similar structure, such as the one you have in your example. You only need to create additional axes (subplot) and a second line object:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def data_gen():
t = data_gen.t
cnt = 0
while cnt < 1000:
cnt+=1
t += 0.05
y1 = np.sin(2*np.pi*t) * np.exp(-t/10.)
y2 = np.cos(2*np.pi*t) * np.exp(-t/10.)
yield t, y1, y2
data_gen.t = 0
fig, (ax1, ax2) = plt.subplots(2,1)
line1, = ax1.plot([], [], lw=2)
line2, = ax2.plot([], [], lw=2, color='r')
line = [line1, line2]
for ax in [ax1, ax2]:
ax.set_ylim(-1.1, 1.1)
ax.set_xlim(0, 5)
ax.grid()
xdata, y1data, y2data = [], [], []
def run(data):
t, y1, y2 = data
xdata.append(t)
y1data.append(y1)
y2data.append(y2)
for ax in [ax1, ax2]:
xmin, xmax = ax.get_xlim()
if t >= xmax:
ax.set_xlim(xmin, 2*xmax)
ax.figure.canvas.draw()
line[0].set_data(xdata, y1data)
line[1].set_data(xdata, y2data)
return line
ani = animation.FuncAnimation(fig, run, data_gen, blit=True, interval=10,
repeat=False)
plt.show()
source
share