анимированные сюжеты с использованием matplotlib и информационного текста с текущими значениями - PullRequest
0 голосов
/ 22 ноября 2018

Я пытаюсь объединить примеры из здесь и здесь .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()

1 Ответ

0 голосов
/ 22 ноября 2018

Я думаю, вы хотите вернуть итерацию художников для анимации.Это должен быть один итеративный, хотя.Например,

return lines + info_text
...