用单位给线图刻面

时间:2018-11-07 04:26:02

标签: seaborn

我有一个数据框,其中包含我研究中几个人的数据。这些人属于四个组之一。我想绘制每个人的曲线,并将其与该组中的其他人进行比较。

我希望能按小组讨论,然后使用units参数为lineplot中的每个人画线。

这是我到目前为止所拥有的:

g = sns.FacetGrid(data = m, col='Sex', row = 'Group')
g.map(sns.lineplot, 'Time','residual')

文档说g.map按照出现在lineplot中的顺序接受参数。 units在很长的列表的末尾。

如何刻划线图并使用units参数?

这是我的数据:

Subject Time    predicted   Concentration   Group   Sex residual
1   0.5 0.24    0.01    NAFLD   Male    -0.23
1   1.0 0.4 0.33    NAFLD   Male    -0.08
1   2.0 0.58    0.8 NAFLD   Male    0.22
1   4.0 0.59    0.59    NAFLD   Male    -0.0
1   6.0 0.47    0.42    NAFLD   Male    -0.04
1   8.0 0.33    0.23    NAFLD   Male    -0.1
1   10.0    0.22    0.16    NAFLD   Male    -0.06
1   12.0    0.15    0.33    NAFLD   Male    0.18
3   0.5 0.26    0.08    NAFLD   Female  -0.18
3   1.0 0.45    0.45    NAFLD   Female  0.01
3   2.0 0.66    0.7 NAFLD   Female  0.03
3   4.0 0.74    0.76    NAFLD   Female  0.02
3   6.0 0.62    0.7 NAFLD   Female  0.08
3   8.0 0.46    0.4 NAFLD   Female  -0.06
3   10.0    0.32    0.27    NAFLD   Female  -0.05
3   12.0    0.21    0.21    NAFLD   Female  -0.0
4   0.5 0.52    0.13    NAFLD   Female  -0.39
4   1.0 0.91    1.18    NAFLD   Female  0.27
4   2.0 1.37    1.03    NAFLD   Female  -0.34
4   4.0 1.55    2.02    NAFLD   Female  0.47
4   6.0 1.32    1.19    NAFLD   Female  -0.13
4   8.0 1.0 0.89    NAFLD   Female  -0.1
4   10.0    0.71    0.66    NAFLD   Female  -0.05
4   12.0    0.48    0.5 NAFLD   Female  0.02
5   0.5 0.46    0.16    NAFLD   Female  -0.3
5   1.0 0.76    0.98    NAFLD   Female  0.22
5   2.0 1.05    1.03    NAFLD   Female  -0.02
5   4.0 1.03    1.06    NAFLD   Female  0.03
5   6.0 0.8 0.77    NAFLD   Female  -0.03
5   8.0 0.57    0.5 NAFLD   Female  -0.07
5   10.0    0.4 0.42    NAFLD   Female  0.02
5   12.0    0.27    0.33    NAFLD   Female  0.06
6   0.5 1.08    1.02    NAFLD   Female  -0.06
6   1.0 1.53    1.66    NAFLD   Female  0.13
6   2.0 1.67    1.52    NAFLD   Female  -0.16
6   4.0 1.3 1.44    NAFLD   Female  0.14
6   6.0 0.94    0.94    NAFLD   Female  -0.0
6   8.0 0.68    0.63    NAFLD   Female  -0.05
6   10.0    0.49    0.36    NAFLD   Female  -0.13
6   12.0    0.35    0.48    NAFLD   Female  0.13
7   0.5 0.5 0.34    Control Female  -0.16
7   1.0 0.81    0.84    Control Female  0.04
7   2.0 1.08    1.17    Control Female  0.1
7   4.0 1.0 0.99    Control Female  -0.01
7   6.0 0.73    0.65    Control Female  -0.08
7   8.0 0.5 0.49    Control Female  -0.01
7   10.0    0.33    0.37    Control Female  0.04
7   12.0    0.22    0.25    Control Female  0.03
8   0.5 0.44    0.37    Control Male    -0.06
8   1.0 0.67    0.74    Control Male    0.07
8   2.0 0.82    0.8 Control Male    -0.03
8   4.0 0.72    0.72    Control Male    0.01
8   6.0 0.54    0.54    Control Male    -0.0
8   8.0 0.4 0.38    Control Male    -0.02
8   10.0    0.29    0.31    Control Male    0.02
8   12.0    0.21    0.21    Control Male    0.0
9   0.5 0.51    0.26    Control Female  -0.25
9   1.0 0.86    0.66    Control Female  -0.21
9   2.0 1.23    1.62    Control Female  0.39
9   4.0 1.3 1.26    Control Female  -0.03
9   6.0 1.07    0.94    Control Female  -0.13
9   8.0 0.81    0.74    Control Female  -0.07
9   10.0    0.59    0.62    Control Female  0.03
9   12.0    0.43    0.54    Control Female  0.11
10  0.5 0.81    0.82    Control Female  0.01
10  1.0 1.05    1.03    Control Female  -0.02
10  2.0 1.04    1.04    Control Female  -0.0
10  4.0 0.77    0.81    Control Female  0.04
10  6.0 0.55    0.52    Control Female  -0.03
10  8.0 0.39    0.35    Control Female  -0.04
10  10.0    0.28    0.31    Control Female  0.03
10  12.0    0.2 0.21    Control Female  0.01
11  0.5 0.08    0.07    NAFLD   Male    -0.01
11  1.0 0.15    0.08    NAFLD   Male    -0.07
11  2.0 0.24    0.13    NAFLD   Male    -0.11
11  4.0 0.32    0.45    NAFLD   Male    0.12
11  6.0 0.33    0.38    NAFLD   Male    0.05
11  8.0 0.3 0.28    NAFLD   Male    -0.02
11  10.0    0.25    0.23    NAFLD   Male    -0.02
11  12.0    0.2 0.16    NAFLD   Male    -0.04
12  0.5 0.72    0.75    NAFLD   Female  0.03
12  1.0 0.84    0.76    NAFLD   Female  -0.08
12  2.0 0.8 0.77    NAFLD   Female  -0.03
12  4.0 0.67    0.74    NAFLD   Female  0.07
12  6.0 0.56    0.65    NAFLD   Female  0.09
12  8.0 0.46    0.48    NAFLD   Female  0.02
12  10.0    0.38    0.34    NAFLD   Female  -0.05
12  12.0    0.32    0.25    NAFLD   Female  -0.07
13  0.5 0.28    0.07    Control Female  -0.21
13  1.0 0.49    0.38    Control Female  -0.1
13  2.0 0.74    0.94    Control Female  0.2
13  4.0 0.88    0.84    Control Female  -0.04
13  6.0 0.77    0.79    Control Female  0.02
13  8.0 0.61    0.57    Control Female  -0.03
13  10.0    0.45    0.44    Control Female  -0.01
13  12.0    0.32    0.32    Control Female  0.01
14  0.5 0.26    0.04    NAFLD   Female  -0.22
14  1.0 0.44    0.35    NAFLD   Female  -0.1
14  2.0 0.64    0.84    NAFLD   Female  0.19
14  4.0 0.68    0.73    NAFLD   Female  0.04
14  6.0 0.54    0.45    NAFLD   Female  -0.1
14  8.0 0.39    0.34    NAFLD   Female  -0.05
14  10.0    0.26    0.26    NAFLD   Female  0.01
14  12.0    0.16    0.24    NAFLD   Female  0.07
15  0.5 0.3 0.11    NAFLD   Male    -0.19
15  1.0 0.49    0.61    NAFLD   Male    0.12
15  2.0 0.67    0.68    NAFLD   Male    0.01
15  4.0 0.64    0.67    NAFLD   Male    0.03
15  6.0 0.48    0.42    NAFLD   Male    -0.06
15  8.0 0.33    0.31    NAFLD   Male    -0.02
15  10.0    0.22    0.26    NAFLD   Male    0.04
15  12.0    0.15    0.17    NAFLD   Male    0.02
16  0.5 0.16    0.05    NAFLD   Male    -0.12
16  1.0 0.26    0.35    NAFLD   Male    0.1
16  2.0 0.33    0.32    NAFLD   Male    -0.01
16  4.0 0.28    0.27    NAFLD   Male    -0.01
16  6.0 0.19    0.17    NAFLD   Male    -0.02
16  8.0 0.12    0.13    NAFLD   Male    0.01
16  10.0    0.07    0.09    NAFLD   Male    0.02
16  12.0    0.05    0.05    NAFLD   Male    0.0
17  0.5 0.32    0.16    NAFLD   Female  -0.16
17  1.0 0.54    0.59    NAFLD   Female  0.06
17  2.0 0.74    0.78    NAFLD   Female  0.04
17  4.0 0.71    0.76    NAFLD   Female  0.05
17  6.0 0.53    0.43    NAFLD   Female  -0.1
17  8.0 0.36    0.35    NAFLD   Female  -0.01
17  10.0    0.23    0.25    NAFLD   Female  0.02
17  12.0    0.15    0.2 NAFLD   Female  0.05
18  0.5 0.49    0.18    Control Female  -0.31
18  1.0 0.81    0.82    Control Female  0.01
18  2.0 1.1 1.27    Control Female  0.16
18  4.0 1.03    1.06    Control Female  0.03
18  6.0 0.72    0.65    Control Female  -0.07
18  8.0 0.45    0.38    Control Female  -0.07
18  10.0    0.26    0.28    Control Female  0.02
18  12.0    0.14    0.19    Control Female  0.04
19  0.5 0.15    0.04    NAFLD   Female  -0.11
19  1.0 0.27    0.21    NAFLD   Female  -0.06
19  2.0 0.43    0.43    NAFLD   Female  -0.01
19  4.0 0.56    0.66    NAFLD   Female  0.1
19  6.0 0.54    0.52    NAFLD   Female  -0.02
19  8.0 0.47    0.48    NAFLD   Female  0.01
19  10.0    0.38    0.38    NAFLD   Female  0.0
19  12.0    0.29    0.24    NAFLD   Female  -0.05
20  0.5 0.38    0.07    NAFLD   Female  -0.31
20  1.0 0.6 0.82    NAFLD   Female  0.22
20  2.0 0.75    0.79    NAFLD   Female  0.04
20  4.0 0.63    0.58    NAFLD   Female  -0.05
20  6.0 0.44    0.39    NAFLD   Female  -0.05
20  8.0 0.29    0.27    NAFLD   Female  -0.02
20  10.0    0.19    0.23    NAFLD   Female  0.04
20  12.0    0.13    0.19    NAFLD   Female  0.07
21  0.5 0.37    0.28    NAFLD   Male    -0.09
21  1.0 0.56    0.66    NAFLD   Male    0.1
21  2.0 0.68    0.64    NAFLD   Male    -0.04
21  4.0 0.59    0.62    NAFLD   Male    0.02
21  6.0 0.45    0.43    NAFLD   Male    -0.02
21  8.0 0.34    0.31    NAFLD   Male    -0.03
21  10.0    0.26    0.29    NAFLD   Male    0.03
21  12.0    0.19    0.2 NAFLD   Male    0.0
22  0.5 0.28    0.21    Control Male    -0.07
22  1.0 0.42    0.5 Control Male    0.08
22  2.0 0.5 0.47    Control Male    -0.03
22  4.0 0.42    0.42    Control Male    0.0
22  6.0 0.31    0.32    Control Male    0.01
22  8.0 0.23    0.22    Control Male    -0.01
22  10.0    0.16    0.17    Control Male    0.01
22  12.0    0.12    0.11    Control Male    -0.01
23  0.5 0.46    0.18    Control Female  -0.28
23  1.0 0.75    0.65    Control Female  -0.1
23  2.0 1.03    1.23    Control Female  0.2
23  4.0 0.96    1.05    Control Female  0.09
23  6.0 0.67    0.58    Control Female  -0.1
23  8.0 0.42    0.36    Control Female  -0.06
23  10.0    0.24    0.22    Control Female  -0.02
23  12.0    0.14    0.14    Control Female  0.0
24  0.5 0.2 0.14    NAFLD   Male    -0.06
24  1.0 0.33    0.41    NAFLD   Male    0.08
24  2.0 0.44    0.4 NAFLD   Male    -0.04
24  4.0 0.41    0.42    NAFLD   Male    0.01
24  6.0 0.31    0.31    NAFLD   Male    0.0
24  8.0 0.22    0.21    NAFLD   Male    -0.01
24  10.0    0.15    0.17    NAFLD   Male    0.02
24  12.0    0.1 0.09    NAFLD   Male    -0.02
25  0.5 0.28    0.05    NAFLD   Female  -0.23
25  1.0 0.48    0.43    NAFLD   Female  -0.05
25  2.0 0.7 0.82    NAFLD   Female  0.12
25  4.0 0.75    0.8 NAFLD   Female  0.06
25  6.0 0.6 0.56    NAFLD   Female  -0.03
25  8.0 0.42    0.38    NAFLD   Female  -0.04
25  10.0    0.28    0.28    NAFLD   Female  -0.0
25  12.0    0.18    0.18    NAFLD   Female  -0.0
26  0.5 0.65    0.38    NAFLD   Female  -0.27
26  1.0 1.0 1.2 NAFLD   Female  0.2
26  2.0 1.23    1.26    NAFLD   Female  0.03
26  4.0 1.0 0.98    NAFLD   Female  -0.02
26  6.0 0.67    0.59    NAFLD   Female  -0.08
26  8.0 0.43    0.42    NAFLD   Female  -0.01
26  10.0    0.27    0.33    NAFLD   Female  0.06
26  12.0    0.17    0.22    NAFLD   Female  0.05
27  0.5 0.1 0.07    NAFLD   Male    -0.02
27  1.0 0.17    0.18    NAFLD   Male    0.02
27  2.0 0.24    0.23    NAFLD   Male    -0.01
27  4.0 0.27    0.3 NAFLD   Male    0.02
27  6.0 0.24    0.22    NAFLD   Male    -0.01
27  8.0 0.19    0.17    NAFLD   Male    -0.01
27  10.0    0.14    0.16    NAFLD   Male    0.01
27  12.0    0.11    0.11    NAFLD   Male    0.0
28  0.5 0.23    0.16    Control Female  -0.08
28  1.0 0.4 0.39    Control Female  -0.01
28  2.0 0.58    0.57    Control Female  -0.01
28  4.0 0.62    0.69    Control Female  0.07
28  6.0 0.49    0.46    Control Female  -0.04
28  8.0 0.35    0.39    Control Female  0.04
28  10.0    0.23    0.18    Control Female  -0.05
28  12.0    0.15    0.12    Control Female  -0.03
29  0.5 0.33    0.24    Control Female  -0.09
29  1.0 0.55    0.5 Control Female  -0.05
29  2.0 0.8 0.86    Control Female  0.06
29  4.0 0.84    0.91    Control Female  0.07
29  6.0 0.66    0.58    Control Female  -0.08
29  8.0 0.46    0.43    Control Female  -0.03
29  10.0    0.3 0.33    Control Female  0.03
29  12.0    0.19    0.2 Control Female  0.01
30  0.5 0.23    0.19    Control Female  -0.04
30  1.0 0.4 0.41    Control Female  0.01
30  2.0 0.6 0.6 Control Female  -0.0
30  4.0 0.68    0.71    Control Female  0.03
30  6.0 0.58    0.56    Control Female  -0.03
30  8.0 0.45    0.43    Control Female  -0.02
30  10.0    0.33    0.36    Control Female  0.02
30  12.0    0.24    0.24    Control Female  0.0
31  0.5 0.36    0.31    Control Female  -0.05
31  1.0 0.61    0.66    Control Female  0.05
31  2.0 0.85    0.82    Control Female  -0.03
31  4.0 0.86    0.9 Control Female  0.05
31  6.0 0.65    0.62    Control Female  -0.03
31  8.0 0.45    0.43    Control Female  -0.02
31  10.0    0.3 0.31    Control Female  0.01
31  12.0    0.19    0.21    Control Female  0.02
32  0.5 0.24    0.14    NAFLD   Male    -0.09
32  1.0 0.4 0.41    NAFLD   Male    0.01
32  2.0 0.56    0.61    NAFLD   Male    0.04
32  4.0 0.57    0.58    NAFLD   Male    0.02
32  6.0 0.43    0.39    NAFLD   Male    -0.04
32  8.0 0.29    0.28    NAFLD   Male    -0.01
32  10.0    0.19    0.2 NAFLD   Male    0.01
32  12.0    0.12    0.14    NAFLD   Male    0.03
33  0.5 0.17    0.05    NAFLD   Male    -0.12
33  1.0 0.28    0.23    NAFLD   Male    -0.06
33  2.0 0.42    0.56    NAFLD   Male    0.14
33  4.0 0.45    0.42    NAFLD   Male    -0.03
33  6.0 0.36    0.33    NAFLD   Male    -0.03
33  8.0 0.26    0.24    NAFLD   Male    -0.02
33  10.0    0.18    0.21    NAFLD   Male    0.03
33  12.0    0.12    0.14    NAFLD   Male    0.02
34  0.5 0.09    0.1 NAFLD   Male    0.01
34  1.0 0.16    0.19    NAFLD   Male    0.03
34  2.0 0.25    0.23    NAFLD   Male    -0.03
34  4.0 0.32    0.32    NAFLD   Male    -0.0
34  6.0 0.32    0.3 NAFLD   Male    -0.02
34  8.0 0.28    0.3 NAFLD   Male    0.02
34  10.0    0.24    0.25    NAFLD   Male    0.02
34  12.0    0.2 0.18    NAFLD   Male    -0.02
35  0.5 0.15    0.02    NAFLD   Female  -0.13
35  1.0 0.27    0.14    NAFLD   Female  -0.14
35  2.0 0.46    0.38    NAFLD   Female  -0.08
35  4.0 0.64    0.8 NAFLD   Female  0.16
35  6.0 0.67    0.74    NAFLD   Female  0.07
35  8.0 0.63    0.61    NAFLD   Female  -0.02
35  10.0    0.55    0.51    NAFLD   Female  -0.04
35  12.0    0.46    0.42    NAFLD   Female  -0.04
36  0.5 0.19    0.12    NAFLD   Female  -0.07
36  1.0 0.32    0.36    NAFLD   Female  0.04
36  2.0 0.47    0.46    NAFLD   Female  -0.01
36  4.0 0.53    0.57    NAFLD   Female  0.04
36  6.0 0.48    0.43    NAFLD   Female  -0.05
36  8.0 0.41    0.39    NAFLD   Female  -0.01
36  10.0    0.34    0.38    NAFLD   Female  0.04
36  12.0    0.28    0.27    NAFLD   Female  -0.01
37  0.5 0.1 0.02    NAFLD   Male    -0.08
37  1.0 0.17    0.1 NAFLD   Male    -0.08
37  2.0 0.28    0.27    NAFLD   Male    -0.01
37  4.0 0.36    0.44    NAFLD   Male    0.08
37  6.0 0.34    0.37    NAFLD   Male    0.03
37  8.0 0.29    0.28    NAFLD   Male    -0.02
37  10.0    0.23    0.22    NAFLD   Male    -0.02
37  12.0    0.18    0.15    NAFLD   Male    -0.03

2 个答案:

答案 0 :(得分:1)

可能的解决方法是定义一个新功能

g = sns.FacetGrid(data = m, col='Sex', row = 'Group')


def f(x,y,z,*args,**kwargs):

    return sns.lineplot(x = x, y = y, units = z, estimator = None, *args, **kwargs)

g.map(f, 'Time','residual','Subject')

答案 1 :(得分:1)

如果使用FacetGrid.map_dataframe,则几乎可以像直接调用lineplot一样传递参数:

g = sns.FacetGrid(data = m, col='Sex', row='Group')
g.map_dataframe(sns.lineplot, x='Time', y='residual', units='Subject', estimator=None)

enter image description here