【问题标题】:Change point color in python plot animation在python绘图动画中更改点颜色
【发布时间】:2021-07-09 16:44:26
【问题描述】:

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

我正在使用来自matplotlib.animationmatplotlibmatplotlib.pyplotFuncAnimation 我已经拥有的:

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

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

【问题讨论】:

    标签: python matplotlib animation plot colors


    【解决方案1】:

    通过一些挖掘和反复试验找到了答案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2018-12-10
      • 2017-07-19
      • 2020-06-09
      • 1970-01-01
      • 2014-08-30
      • 2019-07-26
      相关资源
      最近更新 更多