2016-12-30 21 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

相關問題