2016-06-06 3 views
1

서브 플롯을 채우기 위해 for를 사용하려고하는데, 그렇게 할 수 없습니다. 여기 내 코드의 요약은 다음과 같습니다 편집 1 : 그것은 단지 플롯의 나머지 부분은 비어 3 행 3 열의 영상을 나타내는루프 용 파이썬 서브 플로트

for idx in range(8): 
    img = f[img_set[ind[idx]][0]] 
    patch = img[:,col1+1:col2, row1+1:row2] 
    if idx < 3: 
     axarr[0,idx] = plt.imshow(patch) 
    elif idx <6: 
     axarr[1,idx-3] = plt.imshow(patch) 
    else: 
     axarr[2,idx-6] = plt.imshow(patch) 
path_ = 'plots/test' + str(k) + '.pdf' 
fig.savefig(path_) 

. 어떻게 바꿀 수 있습니까?

+0

최소 동작하는 예제를 확인 : 예를 들어, 예에서

import matplotlib.pyplot as plt fig = plt.figure() for idx in xrange(9): ax = fig.add_subplot(3, 3, idx+1) # this line adds sub-axes ... ax.imshow(patch) # this line creates the image using the pre-defined sub axes fig.savefig('test.png') 

, 그것은 같은 것이 될 수 있습니다. – Chiel

+0

내 문제를 해결하기 위해 코드 덩어리를 자른다. 이미지 세트를로드하는 중이고 pre_defined (row1, row2, col1, col2) 이미지의 일부에만 관심이 있고이 다른 이미지를 서브 플로트에 플롯해야합니다. – user1871528

답변

2

하위 플롯을 만드는 것을 잊었습니다. add_subplot() (http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.add_subplot)을 사용할 수 있습니다.

import matplotlib.pyplot as plt 

fig = plt.figure() 

for idx in xrange(8): 
    ax = fig.add_subplot(3, 3, idx+1) 
    img = f[img_set[ind[idx]][0]] 
    patch = img[:,col1+1:col2, row1+1:row2] 
    ax.imshow(patch) 

path_ = 'plots/test' + str(k) + '.pdf'   
fig.savefig(path_) 
+0

서브 Plot을 생성 한 곳을 복사하여 붙여 넣는 것을 잊어 버렸습니다. 그러나 fig.add_subplot의 해결책은 트릭을 고마워했습니다. – user1871528