gpt4 book ai didi

python - Pandas :如何绘制带有标签的数据框的条形图?

转载 作者:太空狗 更新时间:2023-10-30 02:58:28 24 4
gpt4 key购买 nike

我有以下数据框df:

             timestamp      objectId  result
0 2015-11-24 09:00:00 Stress 3
1 2015-11-24 09:00:00 Productivity 0
2 2015-11-24 09:00:00 Abilities 4
3 2015-11-24 09:00:00 Challenge 0
4 2015-11-24 10:00:00 Productivity 87
5 2015-11-24 10:00:00 Abilities 84
6 2015-11-24 10:00:00 Challenge 58
7 2015-11-24 10:00:00 Stress 25
8 2015-11-24 11:00:00 Productivity 93
9 2015-11-24 11:00:00 Abilities 93
10 2015-11-24 11:00:00 Challenge 93
11 2015-11-24 11:00:00 Stress 19
12 2015-11-24 12:00:00 Challenge 90
13 2015-11-24 12:00:00 Abilities 96
14 2015-11-24 12:00:00 Stress 94
15 2015-11-24 12:00:00 Productivity 88
16 2015-11-24 13:00:00 Productivity 12
17 2015-11-24 13:00:00 Challenge 17
18 2015-11-24 13:00:00 Abilities 89
19 2015-11-24 13:00:00 Stress 13

我想实现如下所示的条形图 Picture taken from here http://pandas.pydata.org/pandas-docs/stable/visualization.htmla,b,c,d 列中有标签的地方 ObjectID y 轴应对应于 result 列x 轴应该是 timestamp 列的分组值。

我尝试了几件事,但没有任何效果。这是最接近的,但是 plot() 方法不通过参数进行任何自定义(例如,kind='bar' 不起作用)。

groups = df.groupby('objectId')
sgb = groups['result']
sgb.plot()

还有其他想法吗?

最佳答案

import seaborn as sns

In [36]:
df.timestamp = df.timestamp.factorize()[0]

In [39]:
df.objectId = df.objectId.map({'Stress' : 'a' , 'Productivity' : 'b' , 'Abilities' : 'c' , 'Challenge' : 'd'})

In [41]:
df
Out[41]:
timestamp objectId result
0 0 a 3
1 0 b 0
2 0 c 4
3 0 d 0
4 1 b 87
5 1 c 84
6 1 d 58
7 1 a 25
8 2 b 93
9 2 c 93
10 2 d 93
11 2 a 19
12 3 d 90
13 3 c 96
14 3 a 94
15 3 b 88
16 4 b 12
17 4 d 17
18 4 c 89
19 4 a 13

In [40]:
sns.barplot(x = 'timestamp' , y = 'result' , hue = 'objectId' , data = df );

enter image description here

关于python - Pandas :如何绘制带有标签的数据框的条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33895034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com