2017-07-20 11 views
2

私はJupyterノートにコードを書いていて、Seabornファセットグリッドを持っていて、4列と3行を持ちたいと思っています。各プロットは、10カ国のリストのうちの異なる国のものです。合計12のグリッドがあり、最後の2つは空ですので、最後の2つのグリッドを取り除く方法はありますか?非常に多くのプロットが一緒に表示されているのを見るのが難しいので、5 x 2の寸法を作るという解法は選択肢ではありません。Seaborn Facetgridの空のグリッドを切り捨てることは可能ですか?

コード:

ucb_w_reindex_age = ucb_w_reindex[np.isfinite(ucb_w_reindex['age'])] 
ucb_w_reindex_age = ucb_w_reindex_age.loc[ucb_w_reindex_age['age'] < 120] 

def ageSeries(country): 
    return ucb_w_reindex_age.loc[ucb_w_reindex_age['country_destination'] == country].age.fillna(value=30).resample('5d').rolling(window=3, min_periods=1).mean() 

def avgAge(country): 
    return ucb_w_reindex_age.loc[ucb_w_reindex_age['country_destination'] == country].age.mean() 

num_plots = 10 
fig, axes = plt.subplots(3, 4,figsize=(20, 15)) 
labels = ["01/10", "09/10", "05/11", "02/12", "10/12", "06/13", "02/14"] 

list_of_dfs = [{'country': item, 'age': ageSeries(item), 'avgAge': avgAge(item)} for item in ['US', 'FR', 'AU', 'PT', 'CA', 'DE', 'ES', 'GB', 'IT', 'NL']] 

colors = ['blue', 'green', 'red', 'orange', 'purple', 'blue', 'green', 'red', 'orange', 'purple'] 
col, row, loop = (0, 0, 0) 
for obj in list_of_dfs: 
    row = math.floor(loop/4) 

    sns.tsplot(data=obj['age'], color=colors[loop], ax=axes[row, col]) 
    axes[row, col].set_title('{}'.format(full_country_names[obj['country']])) 
    axes[row, col].axhline(obj['avgAge'], color='black', linestyle='dashed', linewidth=4) 
    axes[row, col].set(ylim=(20, 65)) 
    axes[row, col].set_xticklabels(labels, rotation=0) 
    axes[row, col].set_xlim(0, 335) 

    if col == 0: 
     axes[row, col].set(ylabel='Average Age') 

    col += 1 
    loop += 1 

    if col == 4: 
     col = 0 

fig.suptitle('Age Over Time', fontsize=30) 
plt.show() 

ファセットグリッド*私が有する画像を知っているが、ここでS.O.でタブーのようですが、実際のコードでこれを配置する方法がないtheresの。

enter image description here

答えて

3

あなたのサンプルコードに示すように、私は、あなたが

fig, axes = plt.subplots(3, 4,figsize=(20, 15)) 

ないseaborn.FacetGridであなたのサブプロットを生成すると仮定します。あなたが最初に必要とするのは、どのプロットを取り除きたいのか、正しいインデックスはaxesで何とか見つけ出すことです。次に、matplotlib.figure.Figure.delaxes()を使用して、不要なサブプロットを削除することができます。ここでの例は次のとおりseaborn.FacetGridから

import matplotlib.pyplot as plt 

fig, axes = plt.subplots(3, 4,figsize=(20, 15)) 
fig.delaxes(axes[2, 2]) 
fig.delaxes(axes[2, 3]) 
plt.show() 

enter image description here

削除サブプロットはやや似ています。わずかなディテールは、あなたがg.axesaxesにアクセスしている:

import matplotlib.pyplot as plt 
import seaborn as sns 

tips = sns.load_dataset("tips") 
g = sns.FacetGrid(tips, col="time", row="smoker", sharex=False, sharey=False) 
g.fig.delaxes(g.axes[1, 1]) 
plt.show() 

enter image description here

+0

ああ、申し訳ありません私はファセットグリッドをサブプロットを使用して、いないですね。あなたのソリューションはうまくいった! Thanks Y. Lou – JBT

+0

@TimothyJosephBaney 'seaborn.FacetGrid'のコードを追加しました。必要に応じてチェックしてください。 –

関連する問題