Как получить заголовок анимации Matplotlib для обновления из списка? - PullRequest
0 голосов
/ 13 мая 2019

Я создаю анимацию matplotlib, но название не изменится.

Я пробовал и set_title, и set_text, но он покажет только последний заголовок в списке.

%matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation

plt.rcdefaults()
x=range(0,3)
fig, ax = plt.subplots()
people = ['Dan', 'Jimmy','']
titleList=['Basketball','Hockey','Baseball']
frame=[[4,9,0],[2,6,0],[2,8,0]]
barcollection = plt.barh(x,frame[0])
ax.set_xlim([0,10])
ax.set_title("Project")
ax.set_xlabel("Knowledge Level")
title = plt.text(0.5,0.95, "Basketball", bbox={'facecolor':'r', 'alpha':0.5, 'pad':5},
                transform=ax.transAxes, ha="center")
plt.yticks(np.arange(len(people)),people)

def animate(i):
    while i<2:
        y=frame[i+1]
        print(y)
        for i, b in enumerate(barcollection):
            title.set_text(str(titleList[i]))
            b.set_width(y[i])

anim=animation.FuncAnimation(fig,animate,repeat=False,blit=False,frames=3,
                             interval=2000)

anim.save('mymovie.mp4',writer=animation.FFMpegWriter(fps=.2))

plt.show()

1 Ответ

1 голос
/ 13 мая 2019

Я удалил цикл while и переместил set_text из цикла.Он должен зависеть от меня, а не от j:

%matplotlib
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import animation

plt.rcdefaults()
x=range(0,3)
fig, ax = plt.subplots()
people = ['Dan', 'Jimmy','']
titleList=['Basketball','Hockey','Baseball']
frame=[[4,9,0],[2,6,0],[2,8,0]]
barcollection = plt.barh(x,frame[0])
ax.set_xlim([0,10])
ax.set_title("Project")
ax.set_xlabel("Knowledge Level")
title = plt.text(0.5,0.95, "Basketball", bbox={'facecolor':'r', 'alpha':0.5, 'pad':5},
                transform=ax.transAxes, ha="center")
plt.yticks(np.arange(len(people)),people)


def animate(i):
    y=frame[i]
    print(y)
    title.set_text(str(titleList[i]))
    for j, b in enumerate(barcollection):
        b.set_width(y[j])

anim=animation.FuncAnimation(fig,animate,repeat=False,blit=False,frames=3,
                             interval=2000)

anim.save('mymovie.mp4',writer=animation.FFMpegWriter(fps=.2))

plt.show()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...