Python 如何多次使用FuncAnimation(按顺序,而不是一起使用)?

Python 如何多次使用FuncAnimation(按顺序,而不是一起使用)?,python,animation,matplotlib,Python,Animation,Matplotlib,是否有某种方法可以在同一代码中多次调用matplotlib.animation.FuncAnimation?我有一个numpy数组列表。每个数组都包含不同数量的坐标。我想在列表中循环并为每个数组中的点绘制动画 我能做些什么来实现这两种不同的场景: 1.保留上一个动画的最后一帧,并在该帧上开始新动画。 2.从上一个动画中删除最后一帧,然后开始新的动画 动画,但在代码开头保持相同的布局设置(包括背景图) 当我尝试在下面这样的for循环中使用FuncAnimation时,只有第一个数组被设置动画。在每

是否有某种方法可以在同一代码中多次调用matplotlib.animation.FuncAnimation?我有一个numpy数组列表。每个数组都包含不同数量的坐标。我想在列表中循环并为每个数组中的点绘制动画

我能做些什么来实现这两种不同的场景: 1.保留上一个动画的最后一帧,并在该帧上开始新动画。 2.从上一个动画中删除最后一帧,然后开始新的动画 动画,但在代码开头保持相同的布局设置(包括背景图)

当我尝试在下面这样的for循环中使用FuncAnimation时,只有第一个数组被设置动画。在每个数组之间,我还需要做一些其他的事情,所以我不能只是去掉for循环,然后将数组合并在一起以设置动画

import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt

mylines = [np.array([[1,2], [3,4]]), np.array([[5,6], [7,8], [9,10]])]

fig, ax = plt.subplots()

ax.set_xlim([-10, 10])
ax.set_ylim([-10, 10])

# There's some code here to set the layout (some background plots etc)
# I omitted it in this question


def update_plot(i, data, myplot):
    myplot.set_data(data[:i, 0], data[:i, 1])
    return myplot,

myplot, = ax.plot([], [], '.', markersize=5)

for k in xrange(len(mylines)):
    data = mylines[k]
    numframes = data.shape[0]+1
    delay = 500

    ani = animation.FuncAnimation(fig, update_plot, frames=numframes,
                                  fargs=(data, myplot), interval=delay,
                                  blit=True, repeat=False)
    plt.show()


    # Do some more stuff in the for loop here, that's not related to
    # animations but related to the current array


    # Then I want to retain the last frame from the animation, and start a new
    # animation on top of it. Also, what should I do if I want to erase the
    # last frame instead (erase only the points plotted by the FuncAnimation,
    # but keep the layout I set at the beginning)
*已编辑代码,因为它有一些语法错误

还尝试了以下内容:


但是它一次绘制了所有的数组,有一些奇怪的闪烁。

函数动画想象成一个计时器。它将以给定的速率和给定的参数调用提供给它的函数。您可以使用为管理动画流而调用的函数,而不是使用两个计时器(您需要管理每个计时器的启动和结束时间)

例如,您可以设置一个帧数,在该帧数处动画期间应发生某些事情:

import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt

mylines = [np.array([[1,2], [3,4]]), np.array([[5,6], [7,8], [9,10]])]

fig, ax = plt.subplots()

ax.set_xlim([-10, 10])
ax.set_ylim([-10, 10])

# There's some code here to set the layout (some background plots etc)
# I omitted it in this question

myplot, = ax.plot([], [], '.', markersize=5)


delay = 500
breaking = 1 

def update_plot(i, data, myplot):
    myplot.set_data(data[:i, 0], data[:i, 1])
    if i == breaking:
        # do some other stuff
        print("breaking")
        myplot.set_color("red")
    return myplot,

data = np.append(mylines[0],mylines[1], axis=0)

ani = animation.FuncAnimation(fig, update_plot, frames=data.shape[0],
                              fargs=(data, myplot), interval=delay,
                              blit=True, repeat=False)
plt.show()

当然,这可能是任意复杂的。例如,请参见此问题:,其中动画的方向在用户输入时反转。但是,它仍然使用一个
FuncAnimation

FuncAnimation
作为计时器。它将以给定的速率和给定的参数调用提供给它的函数。您可以使用为管理动画流而调用的函数,而不是使用两个计时器(您需要管理每个计时器的启动和结束时间)

例如,您可以设置一个帧数,在该帧数处动画期间应发生某些事情:

import numpy as np
import matplotlib.animation as animation
import matplotlib.pyplot as plt

mylines = [np.array([[1,2], [3,4]]), np.array([[5,6], [7,8], [9,10]])]

fig, ax = plt.subplots()

ax.set_xlim([-10, 10])
ax.set_ylim([-10, 10])

# There's some code here to set the layout (some background plots etc)
# I omitted it in this question

myplot, = ax.plot([], [], '.', markersize=5)


delay = 500
breaking = 1 

def update_plot(i, data, myplot):
    myplot.set_data(data[:i, 0], data[:i, 1])
    if i == breaking:
        # do some other stuff
        print("breaking")
        myplot.set_color("red")
    return myplot,

data = np.append(mylines[0],mylines[1], axis=0)

ani = animation.FuncAnimation(fig, update_plot, frames=data.shape[0],
                              fargs=(data, myplot), interval=delay,
                              blit=True, repeat=False)
plt.show()

当然,这可能是任意复杂的。例如,请参见此问题:,其中动画的方向在用户输入时反转。但是,它仍然使用一个
FuncAnimation

动画的具体用途是什么?也许你可以一步一步地解释应该发生什么?@ImportanceOfBeingErnest我不确定如何更清楚地解释它,但我会尝试:我有一个numpy数组列表(在我在问题中发布的代码中,这个列表称为myline)。每个数组(在“mylines”列表中)都包含我要绘制的点的坐标。。。每个阵列中有不同数量的点。我想在列表中循环并为每个数组中的点绘制动画(每一帧都会向图形中添加一个新点)。@ImportanceOfBeingErnest当我尝试我在问题中键入的代码时,它只为mylines列表中第一个数组的绘制设动动画,然后它就停止了。我尝试使用断点调试代码,它确实成功地通过了for循环。。。它永远不会播放下一个数组的动画,即使每次循环时都会调用FuncAnimation和plt.show()。@importantanceofbeingernest所以我想知道如何运行动画(逐个向图形添加点),使动画的最后一帧保持在图形上(绘制所有点时,将点保留在图形上),并在该帧顶部运行新的不同动画(在第一个动画完成后)。作为另一个问题,我还想知道是否可以清除第一个动画绘制的点,但保留布局(轴限制、背景打印等)在第一个动画运行之前设置,然后在第一个动画运行之后运行新动画finishes@ImportanceOfBeingErnest我还尝试用for循环而不是FuncAnimation来为单个数组设置动画,但速度太慢了。动画的目的是什么?也许你可以一步一步地解释应该发生什么?@ImportanceOfBeiNest我不确定如何更清楚地解释它,但我会尝试:我有一个numpy数组列表(这个列表在我在问题中发布的代码中称为myline)。每个数组(在myline列表中)包含我要绘制的点的坐标…每个数组中有不同数量的点。我要循环列表并为绘制每个数组中的点设置动画(每帧向图形添加一个新点).@ImportanceOfBeingErnest当我尝试我在问题中键入的代码时,它只会为mylines列表中第一个数组的打印设置动画,然后它就停止了。我尝试使用断点调试代码,它成功地通过了for循环…它只是永远不会播放下一个数组的动画,即使FuncAnimation和plt。每次循环时都会调用show()。@ImportantanceOfBeingernest所以我想知道如何运行动画(将点逐个添加到图形中),使动画的最后一帧保留在图形上(绘制所有点时,将点保留在图形上),并在该帧上运行新的不同动画(第一个动画完成后)。作为另一个问题,我还想知道是否可以清除第一个动画绘制的点,但保留布局(轴限制、背景打印等)在第一个动画运行之前设置,然后在第一个动画运行之后运行新动画finishes@ImportanceOfBeingErnest我还尝试使用for循环而不是FuncAnimation来设置单个阵列的动画,但速度太慢了。