【问题标题】:Pandas: How to plot a barchar with dataframes with labels?Pandas:如何绘制带有标签的数据框的条形图?
【发布时间】:2016-02-26 23:24:41
【问题描述】:

我有以下数据框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

我想实现如下的条形图ObjectID 列中将有标签而不是a,b,c,d,y 轴应对应于result 列,x 轴应为timestamp 列分组的值。

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

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

还有什么想法吗?

【问题讨论】:

  • 你能确切地指定你想要绘制的内容吗?因为你展示的图似乎和你展示的数据没有关系。什么应该用不同的颜色,条形应该按哪个变量分组?
  • 你好joris,谢谢你的回答。我编辑了我的问题。我希望这个问题现在已经足够清楚了

标签: python pandas matplotlib visualization seaborn


【解决方案1】:
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 );

【讨论】:

  • 你的回答很好。谢谢,但是joris给出的解决方案也确实有效。
【解决方案2】:

@NaderHisham 的答案是一个非常简单的解决方案!
但是作为参考,如果你因为某种原因不能使用 seaborn,这是一个纯 pandas/matplotlib 的解决方案:

你需要重塑你的数据,所以不同的 objectIds 变成了列:

In [20]: df.set_index(['timestamp', 'objectId'])['result'].unstack()
Out[20]:
objectId   Abilities  Challenge  Productivity  Stress
timestamp
09:00:00           4          0             0       3
10:00:00          84         58            87      25
11:00:00          93         93            93      19
12:00:00          96         90            88      94
13:00:00          89         17            12      13

如果你对此做一个条形图,你会得到想要的结果:

In [24]: df.set_index(['timestamp', 'objectId'])['result'].unstack().plot(kind='bar')
Out[24]: <matplotlib.axes._subplots.AxesSubplot at 0xc44a5c0>

【讨论】:

  • 非常感谢您完美地回答了我的回答。
猜你喜欢
  • 2016-02-12
  • 2020-12-13
  • 2018-09-28
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
相关资源
最近更新 更多