在循环语句中画出多个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()

结果图形:

python 循环中使用多个subplot画子图像(python matplotlib use more than one subplot in loop)python 循环中使用多个subplot画子图像(python matplotlib use more than one subplot in loop)

 

相关文章:

  • 2021-08-19
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2021-07-14
  • 2021-11-25
  • 2021-04-15
  • 2022-12-23
猜你喜欢
  • 2021-07-11
  • 2022-12-23
  • 2021-09-25
  • 2021-12-02
  • 2022-12-23
  • 2021-12-04
  • 2021-07-27
相关资源
相似解决方案