日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python绘图subplot绘制5幅图:以2行绘制,首行3幅图,次行2幅图居中(内含绘制3幅图简单版)

發布時間:2025/4/5 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python绘图subplot绘制5幅图:以2行绘制,首行3幅图,次行2幅图居中(内含绘制3幅图简单版) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 省時版本解決方法
      • 遇到的問題
      • 解決方法
      • 參考

省時版本解決方法

請使用matplotlib.gridspec

import matplotlib import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt gs = gridspec.GridSpec(2, 6) # 創立2 * 6 網格 gs.update(wspace=0.8) # 對第一行進行繪制 ax1 = plt.subplot(gs[0, :2]) # gs(哪一行,繪制網格列的范圍) ax2 = plt.subplot(gs[0, 2:4]) ax3 = plt.subplot(gs[0, 4:6]) # 對第二行進行繪制 ax4 = plt.subplot(gs[1, 1:3]) ax5 = plt.subplot(gs[1, 3:5]) plt.show()

繪制5幅圖,以2行繪制,首行3幅圖,次行2幅圖居中:

遇到的問題

在研讀《數字圖像處理與python實現》一書,做實驗時需要繪制如下圖所示的5幅圖,自己的印象中subplot繪圖只能上下對齊,所以下圖的繪制遇到問題。

來源:岳亞偉《數字圖像處理與python實現》

解決方法

根據參考1,習得了繪制3幅圖,以2行繪制,單幅圖像居中的方法。

import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec gs = gridspec.GridSpec(2, 4) gs.update(wspace=0.5) ax1 = plt.subplot(gs[0, :2], ) ax2 = plt.subplot(gs[0, 2:]) ax3 = plt.subplot(gs[1, 1:3]) plt.show()

引用:您可以使用gridspec.Gridspec()。這里的技巧是創建一個規則的網格,但不是在每個單元格中都有一個繪圖。例如,在本例中,我創建了一個2x4單元格網格。每個繪圖跨越2個單元格。所以在第一行,我有兩個圖(2x2個單元格)在第二行,我有一個空單元格,一個繪圖(1x2個單元格)和另一個空單元格。
源:見參考1

繪制3幅圖,以2行繪制,單幅圖像居中實驗結果:

然后去官方文檔(詳見參考2)巡視了一圈,然后嘗試變成繪制5幅圖,以2行繪制,首行3幅圖,次行2幅圖居中

import matplotlib.gridspec as gridspec import matplotlib.pyplot as plt gs = gridspec.GridSpec(2, 6) gs.update(wspace=0.8) ax1 = plt.subplot(gs[0, :2]) ax2 = plt.subplot(gs[0, 2:4]) ax3 = plt.subplot(gs[0, 4:6]) ax4 = plt.subplot(gs[1, 1:3]) ax5 = plt.subplot(gs[1, 3:5]) plt.show()

繪制5幅圖,以2行繪制,首行3幅圖,次行2幅圖居中實驗結果:

繪制結果

實驗代碼

from scipy import ndimage from skimage import data, util from matplotlib import pyplot as plt import matplotlib.gridspec as gridspecimg = data.astronaut()[:, :, 0] # 對圖像加入胡椒噪聲 pepper_img = util.random_noise(img, mode='pepper', seed=None, clip=True) # 對圖像加入鹽粒噪聲 salt_img = util.random_noise(img, mode='salt', seed=None, clip=True) n = 3 # 最大值濾波 max_img = ndimage.maximum_filter(pepper_img, (n, n)) # 最小值濾波 min_img = ndimage.minimum_filter(salt_img, (n, n)) # 顯示圖像 plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文 gs = gridspec.GridSpec(2, 6) gs.update(wspace=0.8)ax1 = plt.subplot(gs[0, :2]) ax1.imshow(img,cmap='gray') ax1.set_title("宇航員原圖")ax2 = plt.subplot(gs[0, 2:4]) ax2.imshow(pepper_img,cmap='gray') ax2.set_title("加胡椒噪聲圖像")ax3 = plt.subplot(gs[0, 4:6]) ax3.imshow(salt_img,cmap='gray') ax3.set_title("加鹽粒噪聲圖像")ax4 = plt.subplot(gs[1, 1:3]) ax4.imshow(max_img,cmap='gray') ax4.set_title("最大值濾波結果")ax5 = plt.subplot(gs[1, 3:5]) ax5.imshow(min_img,cmap='gray') ax5.set_title("最小值濾波結果")plt.show()

參考

[1]python - Matplotlib:3幅圖以2行繪制,單幅圖像居中
[2]官方文檔:Customizing Figure Layouts Using GridSpec and Other Functions

總結

以上是生活随笔為你收集整理的python绘图subplot绘制5幅图:以2行绘制,首行3幅图,次行2幅图居中(内含绘制3幅图简单版)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。