2016-08-09 240 views
1

我有一個如下所示的數據框:大熊貓繪製一個特定的列作爲x軸和y軸使用?

我想plot company_id作爲x軸,count(company_id)作爲y軸。 我也想堆積它的類別openclose

該代碼不顯示任何輸出並無限運行。

df 
     person_id company_id time event type date 
    0   1 255  1379312026 open A 2013-09-16 02:13:46 
    1   1 255  1379312086 close A 2013-09-16 02:14:46 
    2   1 182  1379312926 open B 2013-09-16 02:28:46 
    3   1 182  1379313046 close B 2013-09-16 02:30:46 
    4   1 81  1379314006 open A 2013-09-16 02:46:46 


df2=df[['company_id','event']] 
df2.plot(kind='bar',stacked=True) 
+0

會推薦Seaborn這一點。他們有方法可以在繪圖時隨時進行小型計算。看看這個Seaborn示例頁面:https://stanford.edu/~mwaskom/software/seaborn/tutorial/categorical.html – Kartik

回答

2

如果你能使用seaborn的建議在評論你可以這樣做:

import seaborn as sns, matplotlib.pyplot as plt 

df = {'company_id': {0: 255, 1: 255, 2: 182, 3: 182, 4: 81}, 
     'time': {0: 1379312026, 1: 1379312086, 2: 1379312926, 3: 1379313046, 4: 1379314006}, 
     'person_id': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1}, 
     'counts': {0: 2, 1: 2, 2: 2, 3: 2, 4: 1}, 
     'type': {0: 'A', 1: 'A', 2: 'B', 3: 'B', 4: 'A'}, 
     'event': {0: 'open', 1: 'close', 2: 'open', 3: 'close', 4: 'open'}} 

# add counts column  
counts = df.groupby('company_id').size().rename('counts') 
df['count'] = df['company_id'].map(counts) 

g = sns.factorplot(y='count',x='company_id',hue='event',data=df,kind='bar', 
        palette='muted',legend=False,ci=None) 
plt.legend(loc='best') 
plt.show() 

結果:

enter image description here

相關問題