【问题标题】:Animation of categorical data with matplotlib.animation.FuncAnimation使用 matplotlib.animation.FuncAnimation 对分类数据进行动画处理
【发布时间】:2020-07-02 19:59:30
【问题描述】:

我尝试使用matplotlib.animation.FuncAnimation 对分类数据进行动画处理。但是,我还没有找到根据table 的类别为动画x y 值着色的方法。这是我的数据集 DF:

         x      y  time   table
0    150.0  126.0     0  Table3
1    160.0  244.0     0  Table1
2    153.0  126.0     1  Table3
3    159.0  244.0     1  Table1
4    154.0  131.0     2  Table3
5    161.0  243.0     2  Table1
6    149.0  131.0     3  Table3
7    158.0  244.0     3  Table1
8    394.0  247.0     4  Table4
9    150.0  128.0     4  Table3
10   161.0  243.0     4  Table1
11   160.0  244.0     5  Table1
12   154.0  130.0     5  Table3
13   153.0  129.0     6  Table3
14   163.0  237.0     6  Table1
15   134.0  128.0     7  Table3
16   162.0  238.0     7  Table1
17   162.0  237.0     8  Table1
18   162.0  240.0     9  Table1
19   163.0  239.0    10  Table1
20   164.0  239.0    11  Table1
21   165.0  239.0    12  Table1
22   266.0  195.0    13  Table2
23   163.0  240.0    13  Table1
24   165.0  239.0    14  Table1
25   164.0  239.0    15  Table1
26   165.0  239.0    16  Table1
27   165.0  240.0    17  Table1
28   394.0  259.0    17  Table4
29   151.0  129.0    18  Table3

有没有人建议如何根据table 列为动画着色?我玩弄了这段代码,但还没有找到办法。任何帮助表示赞赏!

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation

title = 'Occ'
x = np.array(df.x)
y = np.array(df.y)
occupancy = pd.DataFrame(y,x)

occupancy.columns = {title}

Writer = animation.writers['ffmpeg']
writer = Writer(fps=20, metadata=dict(artist='Me'), bitrate=1800)

fig = plt.figure(figsize=(12,8))
plt.xlim(0, 500)
plt.ylim(0,500)
plt.xlabel('X',fontsize=20)
plt.ylabel('Y',fontsize=20)
plt.title('Occ',fontsize=20)


def animate(i):
    data = occupancy.iloc[:int(i+1)] #select data range
    p = sns.scatterplot(x=data.time, y=data[title], data=df, color="Blue")
    p.tick_params(labelsize=17)
    plt.setp(p.lines,linewidth=7)
    
ani = matplotlib.animation.FuncAnimation(fig, animate, frames=400, repeat=True)
ani.save('Occ.mp4', writer=writer)

【问题讨论】:

    标签: python pandas matplotlib animation matplotlib-animation


    【解决方案1】:

    您可以使用sns.scatterplot 中的hue 参数以相同的颜色绘制同一张表的点。检查此代码:

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    
    df = pd.read_csv('data.csv')
    
    title = 'Occ'
    x = np.array(df.x)
    y = np.array(df.y)
    
    Writer = animation.writers['ffmpeg']
    writer = Writer(fps = 20, metadata = dict(artist = 'Me'), bitrate = 1800)
    
    fig = plt.figure(figsize = (12, 8))
    
    def animate(i):
        plt.gca().cla()
        data = df.iloc[:int(i + 1)]  # select data range
        p = sns.scatterplot(x = 'x', y = 'y', hue = 'table', data = data, s = 200, alpha = 0.5)
        p.tick_params(labelsize = 17)
        plt.setp(p.lines, linewidth = 7)
        plt.xlim(0, 500)
        plt.ylim(0, 500)
        plt.xlabel('X', fontsize = 20)
        plt.ylabel('Y', fontsize = 20)
        plt.title('Occ', fontsize = 20)
    
    ani = matplotlib.animation.FuncAnimation(fig, animate, frames = len(df), repeat = True)
    ani.save('Occ.mp4', writer = writer)
    
    plt.show()
    

    这给了我这个动画:

    我更改了一些代码(我删除了原始数据帧的 occupancy 数据帧和 plottet xy 列)以便正确运行动画。您可以在代码中实现hue = 'table' 参数。

    【讨论】:

      猜你喜欢
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      相关资源
      最近更新 更多