我正在尝试使用for lop来填充子图,但我不能这样做。这是我的代码的摘要: 编辑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_)
它仅绘制第3行和第3列上的图像,其余部分为空白。我怎么能改变它?
答案 0 :(得分: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(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')
在您的示例中,它可能类似于:
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_)