2014-06-26 30 views
1

我有兩個字段「market_cap」和「debt_ratio」,其中我使用熊貓切功能來創建5個基於market_cap的子組。使用matplotlib與子組的一個陰謀中的多個箱圖使用

我有興趣創建5個盒子圖(對於每個子羣),但要顯示的數據是debt_ratio。

cleaned_data = (cleaned_data.groupby(pd.cut(cleaned_data['market_cap_(in_us_$)'], 5  ))['market_debt_to_capital_ratio']) 

# Create a figure instance 
fig = plt.figure(1, figsize = (9, 5)) 

# Create an axes instance 
ax = fig.add_subplot(111) 

# Create the boxplot 
bp = ax.boxplot(cleaned_data) 

# Save the figure 
fig.savefig('fig1.png', bbox_inches = 'tight') 

不過,我發現了以下錯誤

File "C:...\box_plots.py", line 29, in <module> 
bp = ax.boxplot(cleaned_data[1]) 
File "C:\Python27\lib\site-packages\pandas\core\groupby.py", line 489, in __getitem__ 
raise NotImplementedError 
NotImplementedError 
+0

您確定這與實際繪圖有關嗎?如果你把'a = cleaned_data [1]'作爲你的第二行代碼 - 它會拋出一個異常嗎? – Korem

+0

下面的答案解決了我的問題。除此之外,我還沒有完全理解你對「a = cleaned_data [1]」的建議。我再次運行上面的代碼,現在我得到: 文件「C:\ ... \ box_plots.py」,第32行,在 bp = ax.boxplot(cleared_data) 文件「C:\ Python27 \如果不是hasattr(x [0],'__len__'): 文件「C:\ Python27 \ lib \ site-packages \ pandas \ core」,則可以使用lib \ site-packages \ matplotlib \ axes.py「,第5967行,在箱線圖 \ groupby.py「,第489行,在__getitem__ raise NotImplementedError – user3724295

回答

1

您可以通過將各個組到列表產生的箱線圖。

test = [] 
for name, group in cleaned_data: 
    test.append(group) 
boxplot(test)