subplot将多张图片画到一张图里

    科技2026-02-10  21

    一个figure对象包含了多个子图,可以使用subplot()函数来绘制子图:

    subplot(numbRow , numbCol ,plotNum ) 或者 subplot(numbRow numbCol plotNum)

    numbRow是plot图的行数;numbCol是plot图的列数;plotNum是指第几行第几列的第几幅图 ;位序从1开始。顺序是从左到右,从上到下 #画图-subplot把四个图放在一张图上 plt.figure(figsize=(15,15)) plt.subplot(2,2,1) #plt.plot()实际上会通过plt.gca()获得当前的Axes对象ax,然后再调用ax.plot()方法实现真正的绘图。 ax = plt.gca() plt.plot(rowData1,color = 'green') ax.set_xlabel('Pixel Number') ax.set_ylabel('Gray Value') ax.set_title("cameraman.tif's row") plt.subplot(2,2,2) ax = plt.gca() plt.plot(columnData1,color = 'red') ax.set_xlabel('Pixel Number') ax.set_ylabel('Gray Value') ax.set_title("cameraman.tif's column") plt.subplots_adjust(hspace = 0.5) plt.subplot(2,2,3) ax = plt.gca() plt.plot(rowData2,color = 'green') ax.set_xlabel('Pixel Number') ax.set_ylabel('Gray Value') ax.set_title("einstein.tif's row") plt.subplot(2,2,4) ax = plt.gca() plt.plot(columnData2,color = 'red') ax.set_xlabel('Pixel Number') ax.set_ylabel('Gray Value') ax.set_title("einstein.tif's column") plt.show()
    其中调整子图间的间距 subplots_adjust(left=None, bottom=None, right=None, top=None,wspace=None, hspace=None) # 参数说明: top、bottom、left、right:整个图距离上下左右边框的距离 wspace、hspace:这个才是调整各个子图之间的间距 wspace:调整子图之间的横向间距 hspace:调整子图之间纵向间距 设置窗口尺寸大小 plt.figure(figsize=(a, b)) #参数说明: a为图形的宽, b为图形的高,单位为英寸。

    效果:


    plt.figure(figsize=(10,5)) #设置窗口大小 plt.suptitle('Multi_Image') # 图片名称 plt.subplot(2,3,1) plt.title('image') plt.imshow(img) plt.axis('off') plt.subplot(2,3,2) plt.title('gray') plt.imshow(gray,cmap='gray') #这里显示灰度图要加cmap plt.axis('off') ... plt.show() #只需要加一个
    Processed: 0.012, SQL: 9