2016-12-29 90 views
1

我有這樣插曲爲seaborn箱線圖

import seaborn as sns 
import pandas as pd 
%pylab inline 
df = pd.DataFrame({'a' :['one','one','two','two','one','two','one','one','one','two'], 'b': [1,2,1,2,1,2,1,2,1,1], 'c': [1,2,3,4,6,1,2,3,4,6]}) 

單箱線圖一個數據幀是OK

sns.boxplot( y="b", x= "a", data=df, orient='v') 

但我想建立所有變量的插曲。我做

names = ['b', 'c'] 
plt.subplots(1,2) 
sub = [] 
for name in names: 

    ax = sns.boxplot( y=name, x= "a", data=df, orient='v') 
    sub.append(ax) 

,我也得到

enter image description here

如何解決呢? thanx尋求幫助

回答

5

我們創建的數字與次要情節:

f, axes = plt.subplots(1, 2) 

凡軸與每個插曲的數組。

然後我們通過參數ax告訴每個我們想要他們的子圖的情節。

sns.boxplot( y="b", x= "a", data=df, orient='v' , ax=axes[0]) 
sns.boxplot( y="c", x= "a", data=df, orient='v' , ax=axes[1]) 

,其結果是:

enter image description here

0
names = ['b', 'c'] 
fig, axes =plt.subplots(1,2) 

for i,t in enumerate(names): 

    sns.boxplot( y=t, x= "a", data=df, orient='v',ax=axes[i % 2]) 

P.S. @Fabian Schultz問我爲什麼要解決問題。我改變了我的代碼如下

names = ['b', 'c'] 
fig, axes =plt.subplots(1,2) 
sns.set_style("darkgrid") 
flatui = [ "#95a5a6", "#34495e"] 
for i,t in enumerate(names): 

    sns.boxplot( y=t, x= "a", data=df, orient='v',ax=axes[i % 2] , palette= flatui) 

enter image description here

這工作。或者可能是我不明白你的問題

+1

您可以加入一個解釋,爲什麼你的解決方案應該工作? –

+0

@FabianSchultz我回答,見上文 – Edward