python 循環中使用多個subplot畫子圖像(python matplotlib use more than one subplot in loop)


在循環語句中畫出多個subplot圖像代碼如下

http://jonathansoma.com/lede/data-studio/classes/small-multiples/long-explanation-of-using-plt-subplots-to-create-small-multiples/

https://www.howtobuildsoftware.com/index.php/how-do/mww/python-loops-matplotlib-subplot-subplots-in-matplotlib-creating-a-loop

import cv2 import matplotlib.pyplot as plt import os import numpy as np path_image = './picture' row_sum = [] count = 1 fig1 = plt.figure() fig2 = plt.figure() for i in ['packing_list.png', 'packing_list90.png', 'packing_list180.png', 'packing_list270.png']: # 計算水平投影,即投影到y軸
    path_image_current = os.path.join(path_image, i) image = cv2.imread(path_image_current, 0) binary = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1] row_sum_per = np.sum(binary, axis=1).tolist() row_sum.append(row_sum_per) # 畫圖
    ax1 = fig1.add_subplot(2, 2, count) ax1.imshow(image, 'gray') ax2 = fig2.add_subplot(2, 2, count) ax2.plot(row_sum_per) count = count + 1 plt.show()

結果圖形:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM