2016-12-30 44 views
1

我有一个matplotlib的问题。 我需要准备一个由指定目录中的列表组成的图。下面的代码生成该代码,但它省略了第一个路径... 例如,如果我需要准备包含14个子图的图像,则只会复制13个图像,首先会被省略,而不是第一个,最后会有一个空图位置。 我检查过,该函数读取所有路径,包括第一个列表。 如果你能够帮助并给我一个提示,我会做错什么,我将不胜感激。 最好的问候为什么列表中的第一个数字没有绘制,但最后还是有一个空的阴谋?

def create_combo_plot(path_to_dir, list_of_png_abspath): 
    name = path_to_dir.replace('_out', '') 
    title = name 
    if name.find('/') != -1: 
     title = name.split('/')[-1] 
    list_of_png_abspath 
    how_many_figures = len(list_) 
    combo_figure = plt.figure(2, figsize=(100,100)) 
    a = 4 
    b = int(floor(how_many_figures/4.1)) + 1 
    for i, l in enumerate(list_of_png_abspath): 
     print l #I`ve checked, path is reached 
     j = i + 1 
     img=mpimg.imread(l) 
     imgplot = plt.imshow(img, interpolation="nearest") 
     plot = plt.subplot(b, a, j) 
    combo_figure.suptitle(title, fontsize=100) 
    combo_figure.savefig(path_to_dir +'/' + title + '.jpeg') 
    plt.close(combo_figure) 

回答

2

替换这些两行:

imgplot = plt.imshow(img, interpolation="nearest") 
plot = plt.subplot(b, a, j) 

这些:

sub = plt.subplot(b, a, j) 
sub.imshow(img, interpolation="nearest") 

行:

imgplot = plt.imshow(img, interpolation="nearest") 

增加了一个新的阴谋最后一个活动插曲。在你的情况下,它在前面的循环在这里创建:

plot = plt.subplot(b, a, j) 

所以,你开始的第二图像和最后的插曲保持为空。

但是,如果你创建的插曲第一:

sub = plt.subplot(b, a, j) 

后来明确地绘制了进去:

sub.imshow(img, interpolation="nearest") 

你应该看到14个地块。

+0

非常感谢!有用 – fafnir1990