2017-11-07 8 views
0

Gridpec内にGridSpecを作成したいと思います。Gridspec内のGridspec

import matplotlib.pyplot as plt 

for i in range(40): 
    i = i + 1 
    ax1 = plt.subplot(10, 4, i) 
    plt.axis('on') 
    ax1.set_xticklabels([]) 
    ax1.set_yticklabels([]) 
    ax1.set_aspect('equal') 
    plt.subplots_adjust(wspace=None, hspace=None) 
plt.show() 

をしかし、私は40 gridspecsをしたい: 私はすでにここに私のコードのようないくつかのGridSpecsを作成することができます。 InすべてのGridspecは別の21グリッド(inner_grid) であり、すべてのinner_gridは1つ上のグリッドで、6つのグリッドが残りのものを埋める必要があります。 ほぼこのリンクの最後の画像のようです: https://matplotlib.org/tutorials/intermediate/gridspec.html しかし、私はそれを本当に理解していません。

私はこれを試してみました:

import matplotlib as mpl 
from matplotlib.gridspec import GridSpec 
import matplotlib.gridspec as gridspec 
import matplotlib.pyplot as plt 

    fig = plt.figure(figsize=(5,10), dpi=300) 
    ax = plt.subplot(gs[i]) 
    #trying to make multiple gridspec 
    # gridspec inside gridspec 
     outer_grid = gridspec.GridSpec(48, 1, wspace=0.0, hspace=0.0) 

     for i in range(21): 
      #ax = plt.subplot(5, 5, i) 
      inner_grid = gridspec.GridSpecFromSubplotSpec(5, 5, subplot_spec=outer_grid[i], wspace=0.0, hspace=0.0) 
      a, b = int(i/4)+1, i % 4+1 
      for j in enumerate(product(range(1, 4), repeat=2)): 
       ax = plt.Subplot(fig, inner_grid[j]) 
       ax.set_xticks([]) 
       ax.set_yticks([]) 
       fig.add_subplot(ax) 

    all_axes = fig.get_axes() 

答えて

0

あなたはそれはあなたが何をしたいのですか? 1つの図で40 * 21 * 6 = 5040軸... さらに、あなたの説明(各セルに40個のセルと21個の内部セルを持つグリッド)は、48個のセルと25個のセルがある場所で提供されるコードと一致しませんそれぞれ...

どのような場合でも、これは私があなたが記述しているものを生成する方法です。必ずしも中間のAxesオブジェクトを生成する必要はありません。何かをプロットする予定の軸だけを生成します。

最後に、実際に何を達成しようとしているかによって、何千もの軸を作成するよりも良い方法があるはずです。

import matplotlib.gridspec as gridspec 
fig = plt.figure(figsize=(40,100)) 


outer_grid = gridspec.GridSpec(10,4, wspace=0, hspace=0) 

for outer in outer_grid: 
    # ax = fig.add_subplot(outer) 
    # ax.set_xticklabels([]) 
    # ax.set_yticklabels([]) 
    # ax.set_aspect('equal') 

    inner_grid_1 = gridspec.GridSpecFromSubplotSpec(5,5, subplot_spec=outer) 
    for inner in inner_grid_1: 
     # ax1 = fig.add_subplot(inner) 
     # ax1.set_xticklabels([]) 
     # ax1.set_yticklabels([]) 
     # ax1.set_aspect('equal') 

     inner_grid_2 = gridspec.GridSpecFromSubplotSpec(2,6, subplot_spec=inner) 
     ax_top = fig.add_subplot(inner_grid_2[0,:]) # top row 
     for i in range(6): 
      ax2 = fig.add_subplot(inner_grid_2[1,i]) # bottom row 
      ax2.set_xticklabels([]) 
      ax2.set_yticklabels([]) 

plt.show() 
関連する問題