【问题标题】:How do I animate the changes of a single data point's color using matplolib.animation?如何使用 matplotlib.animation 为单个数据点颜色的变化设置动画?
【发布时间】:2021-03-20 08:22:41
【问题描述】:

我正在尝试更改散点图中单个数据点(df 的第 101 个条目)的颜色。如何使用 matplotlib.animation 为更改设置动画?

首先,这是原始散点图:

import matplotlib.animation as animat
fig ,ax = plt.subplots()
fig.set_dpi(100)
fig.set_size_inches(15, 8)

groups = df.groupby('Name')
for name, group in groups:
    ax.plot(group.SepalWidth, 
            group.SepalLength, 
            marker='o', 
            linestyle='',
            label=name)
ax.legend(fontsize=10) # legend position
plt.title('Scatter Plot of Sabotaged Iris Data', fontsize=20)
plt.annotate('Sabotaged Data', xy=(3.31,6.305), xytext=(3.5,6.5), arrowprops = dict(facecolor ='black', shrink = 0.04))
plt.xlabel('Sepal Width', fontsize=14)
plt.ylabel('Sepal Length', fontsize=14)

现在我正在尝试随着帧的变化而改变颜色(可能是红色、橙色、黄色、粉红色、紫色),但我想不出如何填充更新功能:

def update(frame):
   # ???

ani = animat.FuncAnimation(fig, update,
                           frames=360, 
                           interval=0.01,
                           blit=True)

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    首先,您将旧界面(例如plt.plot)与较新的object-oriented interface(将使用ax.plot)混合在一起。对于一起执行的代码,最好坚持使用一个接口。

    您不能为散点图中的单个点指定任意颜色。但是您可以创建一个仅包含一个点的新散点图,并在 update 函数内更改其颜色。如果一个人在动画中不断地创造新的元素,系统的内存就会被填满。因此,通常会更新先前创建的元素。

    还要注意FuncAnimation 的间隔以毫秒为单位。不到 50 毫秒的变化将很难被注意到(而且系统可能无法如此快速地重新生成)。

    import matplotlib.animation as animat
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    df = sns.load_dataset('iris')
    df.rename(columns={'species': 'Name', 'sepal_width': 'SepalWidth', 'sepal_length': 'SepalLength'}, inplace=True)
    fig, ax = plt.subplots()
    fig.set_dpi(100)
    fig.set_size_inches(15, 8)
    
    groups = df.groupby('Name')
    for name, group in groups:
        ax.plot(group.SepalWidth,
                group.SepalLength,
                marker='o',
                linestyle='',
                label=name)
    ax.legend(fontsize=10)  # legend position
    ax.set_title('Scatter Plot of Sabotaged Iris Data', fontsize=20)
    sabotaged = (3.31, 6.305)
    ax.annotate('Sabotaged Data', xy=sabotaged, xytext=(3.5, 6.5), arrowprops=dict(facecolor='black', shrink=0.04))
    
    ax.set_xlabel('Sepal Width', fontsize=14)
    ax.set_ylabel('Sepal Length', fontsize=14)
    scatter_dot = ax.scatter(*sabotaged, s=90, fc='turquoise', zorder=3)
    # scatter_circle = ax.scatter(*sabotaged, s=100, fc='none', ec='red', zorder=3)
    
    def update(frame):
        colors = ['crimson', 'darkorange', 'lime', 'cornflowerblue', 'purple', 'fuchsia']
        scatter_dot.set_color(colors[frame % len(colors)])
        return scatter_dot,
    
    ani = animat.FuncAnimation(fig, update,
                               frames=360,
                               interval=2000,
                               blit=True)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      相关资源
      最近更新 更多