2017-06-08 96 views
0

我想为每个日期/时间组合的DataFrame中的项目数量做一个散点图。我已经分组的数据是这样的:散点图绘制DataFrame分组为2列并有聚合

dff = pd.DataFrame(df.groupby(['date', 'time']).size().rename('count')) 

,它看起来像这样:

      count 
date   time  
2017-05-19 15:00   1 
      15:30   1 
      16:00   1 
      16:30   1 
      17:00   1 
2017-05-23 10:00   2 
      10:30   2 
      11:00   2 
... 

现在,我怎么能散点图有在Y在X轴上date S和time S中的计数轴? plt.scatter(x, y, s=area, c=colors)是签名,但是我尝试从dff中选择xy,但未能找到密钥。另外,scatter预计在轴上浮动,而我有字符串。

回答

1

这需要访问MultiIndex值,就像这样:

# replicating sample data (foo is just dummy data for the count) 
grouped = df.groupby(['date', 'time'])['foo'].count() 
date  time  
2015-01-01 15:00:00 1 
      15:30:00 1 
2015-01-02 16:00:00 2 
Name: foo, dtype: int64 

plt.scatter(x=grouped.index.get_level_values(0), y=grouped.index.get_level_values(1), s=[20*4**n for n in grouped.values]) 
plt.show() 

你需要在scatter()s参数玩,这里是我用的是公司招聘pyplot scatter plot marker size的文档。

enter image description here