でやっていること、それを行うための方法はありません。 9または15のプロットがある場合、それはうまくいくはずです。
次の例では、一つだけのプロットやプロットの1行があります場合でも、あなたは一般的に、インデックス、それをできるように
- 空白余分な空のプロット
- が2D配列であることを軸ポインタを強制する方法を示し
をごループなど、各プロットのための正しい行と列を検索ここでは、次のとおりです。
nplots=13
#find number of columns, rows, and empty plots
nc=int(nplots**0.5)
nr=int(ceil(nplots/float(nc)))
empty=nr*nc-nplots
#make the plot grid
f,ax=pyplot.subplots(nr,nc,sharex=True)
#force ax to have two axes so we can index it properly
if nplots==1:
ax=array([ax])
if nc==1:
ax=ax.reshape(nr,1)
if nr==1:
ax=ax.reshape(1,nc)
#hide the unused subplots
for i in range(empty): ax[-(1+i),-1].axis('off')
#loop through subplots and make output
for i in range(nplots):
ic=i/nr #find which row we're on. If the definitions of ir and ic are switched, the indecies for empty (above) should be switched, too.
ir=mod(i,nr) #find which column we're on
axx=ax[ir,ic] #get a pointer to the subplot we're working with
axx.set_title(i)
これはほとんど正しいです - 私たちは奇数を持っている場合、グリッドの最後に空のプロットがあるだろう除き。それらを防ぐ方法はありますか? – user6605695
空のグリッドスポットは凡例を追加するのに便利です。同様に@EL_DONは、プロットの素数が2つ以上の行や列に均等に分割されることはないということが正しいです。 – esmit