0

I have a list of points, lets say as (x,y) pairs. I am trying to animate a plot so that each frame of the animation, a new point show up on the plot in a different color. Specifically on the 0th frame, the 0th point appears, on the the 1st frame, the 1st point appears, and so on. I would also like to have these points appear in a new color, specifically like a linear progression through a color palette as the points progress, so that you can "follow" the points by their color. This is similar to, and how I got as far as I am now: How can i make points of a python plot appear over time?. The first animation in the link is spot on, except without the points changing colors.

I am using matplotlib, matplotlib.pyplot, and FuncAnimation from matplotlib.animation What I have already:

    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()

I can change the color of all of the points together on each frame by including the line graph.set_color(colors[i]) in the animate function, but not each point individually.

1 Answers1

0

Figured it out with some digging and trial and error:

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()