2017-11-07 1 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 Grid (inner_grid) 이어야하며 모든 inner_grid에는 맨 위에 하나의 격자 여야하며 나머지 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

당신은 당신이 원하는 무엇인지 것입니까? 한 그림에 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() 
관련 문제