0

我有一个点列表,可以说是 (x,y) 对。我正在尝试对情节进行动画处理,以便动画的每一帧,一个新点以不同的颜色显示在情节上。具体到第0帧出现第0个点,第1帧出现第1个点,以此类推。我还想让这些点以一种新的颜色出现,特别是像随着点的进展通过调色板的线性进展,这样你就可以通过它们的颜色“跟随”这些点。这类似于,以及我现在的情况:如何使 python 图的点随着时间的推移出现?. 链接中的第一个动画是正确的,除了没有改变颜色的点。

我正在使用matplotlib,matplotlib.pyplotFuncAnimation来自matplotlib.animation 我已经拥有的内容:

    def plot_points_over_time(list_of_points):
        num_points = len(list_of_points)
        fig = plt.figure()
        x, y = zip(*list_of_points)
        plt.xlim(min(x),max(x))
        plt.ylim(min(y),max(y))
        colors = [plt.cm.gist_rainbow(each) for each in np.linspace(0,1,num_points)]
        graph, = plt.plot([],[],'o')
        def animate(i):
            graph.set_data(x[:i+1],y[:i+1])
            return graph
        ani = FuncAnimation(fig, animate, frames = num_points, repeat = False, interval = 60000/num_points)
        plt.show()

graph.set_color(colors[i])我可以通过在animate函数中包含线来更改每帧上所有点的颜色,但不能单独更改每个点。

4

1 回答 1

0

通过一些挖掘和反复试验弄清楚了:

def plot_points_over_time(list_of_points):
    num_points = len(list_of_points)
    fig = plt.figure()
    x, y = zip(*list_of_points)
    plt.xlim(min(x),max(x))
    plt.ylim(min(y),max(y))
    colors = [plt.cm.gist_rainbow(each) for each in np.linspace(0,1,num_points)]
    scat, = plt.plot([],[])
    def animate(i):
        scat.set_offsets(np.c_[x[:i+1], y[:i+1]])
        scat.set_color(colors[:i+1])
        return scat,
    ani = FuncAnimation(fig, animate, frames = num_points, repeat = False, interval = 60000/num_points)
    plt.show()
于 2021-04-16T13:23:32.447 回答