我试图使用for来填充一个子图,但我做不到。下面是我的代码摘要:编辑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列上绘制图像,其余部分为空白。我怎么才能改变呢?
发布于 2016-06-06 20:28:26
你忘了创建子情节了。您可以使用add_subplot() (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_)https://stackoverflow.com/questions/37666087
复制相似问题