我正在尝试结合here和here的示例。 info_text
不起作用,没有更新。
我遇到错误:
AttributeError: 'list' object has no attribute 'set_animated'
有人知道为什么吗? 这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from pprint import pprint
total_subplots = 2
# initialize the data arrays
#xdata, y1data, y2data = [], [], []
signals =list()
for i in range(0, total_subplots):
signals.append([]) # [[],[]]
xdata = list()
ax = list()
lines = list()
info_text = list()
colors=['blue', 'red']
def data_gen():
t_max = 1000.0
t = 0
dt = 0.05
y = [''] * total_subplots
while t < t_max:
t += dt
y[0] = np.sin(2*np.pi*t) * np.exp(-t/10.)
y[1] = np.cos(2*np.pi*t) * np.exp(-t/10.)
# adapted the data generator to yield both sin and cos
yield t, y
def run(data):
# update the data
t, y = data
xdata.append(t)
for i in range(0, total_subplots):
signals[i].append(y[i])
# axis limits checking. Same as before, just for both axes
for i in range(len(ax)):
xmin, xmax = ax[i].get_xlim()
if t >= xmax:
ax[i].set_xlim(xmin, 2*xmax)
ax[i].figure.canvas.draw()
# update the data of both line objects
for i in range(len(lines)):
lines[i].set_data(xdata, signals[i])
text = 'Time = %.1f s \nValue = %.1f'%(t, y[i])
info_text[i].set_text(text)
return lines, info_text
# Initialize
# create a figure with two subplots
#fig, (ax1, ax2) = plt.subplots(2,1)
fig = plt.figure()
for i in range(0, total_subplots):
axis = fig.add_subplot(2, 1, (i+1))
ax.append(axis)
# intialize two line objects (one in each axes)
line, = ax[i].plot([], [], lw=2, color=colors[i])
lines.append(line)
# the same axes initalizations as before (just now we do it for both of them)
ax[i].set_ylim(-1.1, 1.1)
ax[i].set_xlim(0, 5)
ax[i].grid()
text = ax[i].text(0.05, 0.9, '', transform=ax[i].transAxes)
info_text.append(text)
# Run
ani = animation.FuncAnimation(fig, run,
data_gen,
interval=10,
blit=True,
repeat=False)
plt.show()
答案 0 :(得分:1)
我认为您的意思是返回动画的艺术家迭代。它应该是一个可迭代的。例如
return lines + info_text