Python matplotlib.animation.FuncAnimation никогда не делает повторение второго кадра - PullRequest
0 голосов
/ 27 сентября 2019

Я пытаюсь использовать matplotlib.animation.FuncAnimation для создания пользовательской анимации.Однако функция FuncAnimation, похоже, не выполняет вторую итерацию функции animate.Я приложил простой пример, который я нашел онлайн, который должен работать и рисовать синусоидальную волну.И на моем компьютере, и на сервере Amazon EC2 скрипт вызывает animate и рисует кадр за одну итерацию.Вторая итерация никогда не происходит.Что я не так делаю?

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)


# animation function.  This is called sequentially
def animate(i):
    print("animate invoked")
    print(i)
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, frames=np.arange(100), interval=200)

plt.show()

Вывод скрипта:

animate вызывается

0

enter image description here

1 Ответ

1 голос
/ 27 сентября 2019

В соответствии с примером здесь вам также необходимо передать init_func в FunctionAnimation.Таким образом, вы можете сделать:

# First set up the figure, the axis, and the plot element we want to animate
fig, ax = plt.subplots()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)

# init function
def init():
    return line,

# animation function.  This is called sequentially
def animate(i):
    print("animate invoked")
    x = np.linspace(0, 2, 1000)
    y = np.sin(2 * np.pi * (x - 0.01 * i))
    line.set_data(x, y)
    return line,

# call the animator.  blit=True means only re-draw the parts that have changed.
anim = FuncAnimation(fig, animate, init_func=init, frames=np.arange(100), interval=200)

# for jupyter notebook
HTML(anim.to_html5_video())

Что дает:

enter image description here

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