2016-01-21 18 views
12

私は、各象限に垂直に積み重ねられた2つのサブプロット(2x1グリッド)がある2x2グリッドからなる図形を作成しようとしています。しかし、私はこれを達成する方法を把握していないようです。Matplotlib - サブプロットをサブプロットに追加しますか?

私が得た最も近いものは、gridspecといくつかの醜いコード(下記参照)を使用していますが、gridspec.update(hspace=X)はすべてのサブプロットの間隔を変更します。

理想的には、下の図を例に挙げて、各象限内のサブプロット間の間隔を減らし、上部と下部の象限の間の垂直方向の間隔を広げます(1-3〜2-4 )。

これを行う方法はありますか(gridspecを使用する場合と使用しない場合)。私が最初に想定していたのは、各サブ・サブプロット・グリッド(すなわち各2x1グリッド)を生成し、それらをサブプロットのより大きな2x2グリッドに挿入することですが、サブプロットをサブプロットに追加する方法方法。

あなたが nest your GridSpec using SubplotSpecでき
import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 
plt.figure(figsize=(10, 8)) 
gs = gridspec.GridSpec(4,2) 
gs.update(hspace=0.4) 
for i in range(2): 
    for j in range(4): 
     ax = plt.subplot(gs[j,i]) 
     ax.spines['top'].set_visible(False) 
     ax.spines['right'].set_visible(False) 
     plt.tick_params(which='both', top='off', right='off') 
     if j % 2 == 0: 
      ax.set_title(str(i+j+1)) 
      ax.plot([1,2,3], [1,2,3]) 
      ax.spines['bottom'].set_visible(False) 
      ax.get_xaxis().set_visible(False) 
     else: 
      ax.plot([1,2,3], [3,2,1]) 

答えて

18

enter image description here

。外側のグリッドは2 x 2になり、内側のグリッドは2 x 1になります。次のコードは基本的な考え方を示します。

import matplotlib.pyplot as plt 
import matplotlib.gridspec as gridspec 

fig = plt.figure(figsize=(10, 8)) 
outer = gridspec.GridSpec(2, 2, wspace=0.2, hspace=0.2) 

for i in range(4): 
    inner = gridspec.GridSpecFromSubplotSpec(2, 1, 
        subplot_spec=outer[i], wspace=0.1, hspace=0.1) 

    for j in range(2): 
     ax = plt.Subplot(fig, inner[j]) 
     t = ax.text(0.5,0.5, 'outer=%d, inner=%d' % (i,j)) 
     t.set_ha('center') 
     ax.set_xticks([]) 
     ax.set_yticks([]) 
     fig.add_subplot(ax) 

fig.show() 

enter image description here

+0

完璧な、ありがとう:) – user3014097

関連する問題