首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python子图for循环

python子图for循环
EN

Stack Overflow用户
提问于 2016-06-06 20:13:37
回答 1查看 9.3K关注 0票数 2

我试图使用for来填充一个子图,但我做不到。下面是我的代码摘要:编辑1:

代码语言:javascript
复制
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列上绘制图像,其余部分为空白。我怎么才能改变呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-06 20:28:26

你忘了创建子情节了。您可以使用add_subplot() (subplot)。例如,

代码语言:javascript
复制
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')

在您的示例中,它可能类似于:

代码语言:javascript
复制
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_)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37666087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档