matplotlib - python subplot for loop -


i'm trying use lop populate subplot can't so. here summary of code: edit 1:

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_) 

it plots image on 3rd row , 3rd column rest of plot blank. how can change that?

you forgot create sub plots. can use add_subplot() (http://matplotlib.org/api/figure_api.html#matplotlib.figure.figure.add_subplot). example,

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

in example, like:

import matplotlib.pyplot plt  fig = plt.figure()  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_)