解決python繪圖使用subplots出現(xiàn)標(biāo)題重疊的問題
先上圖

遇到的問題
使用plt.subplots(2,2)繪圖時(shí),子圖的標(biāo)題和上圖重疊,影響觀感:

源代碼:
import numpy as np
from scipy import signal
from skimage import data
from matplotlib import pyplot as plt
# 定義二維灰度圖像的空間濾波函數(shù)
def correl2d(img, window):
# 使用濾波器實(shí)現(xiàn)圖像的空間相關(guān)
# mode = 'same'表示輸出尺寸等于輸入尺寸
# boundary = 'fill'表示濾波前,用常量值填充原始圖像的邊緣,默認(rèn)常量值為0
s = signal.correlate2d(img, window, mode='same', boundary='fill')
return s.astype(np.uint8)
# img為原始圖像
img = data.camera()
# 3*3盒狀濾波模板
window_1 = np.ones((3, 3))/(3 ** 2)
# 5*5盒狀濾波模板
window_2 = np.ones((5, 5))/(5 ** 2)
# 9*9盒狀濾波模板
window_3 = np.ones((9, 9))/(9 ** 2)
# 生成濾波結(jié)果
new_img_1 = correl2d(img, window_1)
new_img_2 = correl2d(img, window_2)
new_img_3 = correl2d(img, window_3)
# 顯示圖像
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文
fig, axs = plt.subplots(2, 2)
axs[0, 0].imshow(img, cmap='gray')
axs[0, 0].set_title("攝影師原圖")
axs[0, 1].imshow(new_img_1, cmap='gray')
axs[0, 1].set_title("3*3盒狀濾波模板")
axs[1, 0].imshow(new_img_2, cmap='gray')
axs[1, 0].set_title("5*5盒狀濾波模板")
axs[1, 1].imshow(new_img_3, cmap='gray')
axs[1, 1].set_title("9*9盒狀濾波模板")
plt.show()
解決方法
方法1:在plt.show() 之前添加一句:
plt.tight_layout()
函數(shù)原型:
matplotlib.pyplot.tight_layout(*, pad=1.08, h_pad=None, w_pad=None, rect=None)
作用:調(diào)整subplots子圖見的間距
Adjust the padding between and around subplots.
參數(shù):

部分代碼:
# 顯示圖像
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文
fig, axs = plt.subplots(2, 2)
axs[0, 0].imshow(img, cmap='gray')
axs[0, 0].set_title("攝影師原圖")
axs[0, 1].imshow(new_img_1, cmap='gray')
axs[0, 1].set_title("3*3盒狀濾波模板")
axs[1, 0].imshow(new_img_2, cmap='gray')
axs[1, 0].set_title("5*5盒狀濾波模板")
axs[1, 1].imshow(new_img_3, cmap='gray')
axs[1, 1].set_title("9*9盒狀濾波模板")
plt.tight_layout()
plt.show()
方法1測(cè)試結(jié)果:

方法2:在subplots中設(shè)置figsize
fig, axs = plt.subplots(2, 2,figsize=(6, 15))
# 顯示圖像
plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文
# 設(shè)置figsize,防止圖片重疊
fig, axs = plt.subplots(2, 2,figsize=(6, 15))
axs[0, 0].imshow(img, cmap='gray')
axs[0, 0].set_title("攝影師原圖")
axs[0, 1].imshow(new_img_1, cmap='gray')
axs[0, 1].set_title("3*3盒狀濾波模板")
axs[1, 0].imshow(new_img_2, cmap='gray')
axs[1, 0].set_title("5*5盒狀濾波模板")
axs[1, 1].imshow(new_img_3, cmap='gray')
axs[1, 1].set_title("9*9盒狀濾波模板")
方法2測(cè)試結(jié)果:

參考
[1]https://blog.csdn.net/txh3093/article/details/106401484
到此這篇關(guān)于python繪圖使用subplots出現(xiàn)標(biāo)題重疊的解決方法的文章就介紹到這了,更多相關(guān)python使用subplots繪圖標(biāo)題重疊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python爬蟲使用正則爬取網(wǎng)站的實(shí)現(xiàn)
這篇文章主要介紹了python爬蟲使用正則爬取網(wǎng)站的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
python利用tkinter實(shí)現(xiàn)圖片格式轉(zhuǎn)換的示例
這篇文章主要介紹了python利用tkinter實(shí)現(xiàn)圖片格式轉(zhuǎn)換,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-09-09
對(duì)python以16進(jìn)制打印字節(jié)數(shù)組的方法詳解
今天小編就為大家分享一篇對(duì)python以16進(jìn)制打印字節(jié)數(shù)組的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
蘋果Macbook Pro13 M1芯片安裝Pillow的方法步驟
Pillow作為python的第三方圖像處理庫,提供了廣泛的文件格式支持,本文主要介紹了蘋果Macbook Pro13 M1芯片安裝Pillow,具有一定的參考價(jià)值,感興趣的可以了解一下2021-11-11
詳解django的serializer序列化model幾種方法
序列化是將對(duì)象狀態(tài)轉(zhuǎn)換為可保持或傳輸?shù)母袷降倪^程。這篇文章主要介紹了詳解django的serializer序列化model幾種方法。具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
詳解Python執(zhí)行py文件是否需要可執(zhí)行權(quán)限
這篇文章主要通過幾個(gè)案例為大家詳細(xì)介紹一下在Python中執(zhí)行py文件是否需要可執(zhí)行權(quán)限,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以了解一下2023-03-03
基于python的opencv圖像處理實(shí)現(xiàn)對(duì)斑馬線的檢測(cè)示例
這篇文章主要介紹了基于python的opencv圖像處理實(shí)現(xiàn)對(duì)斑馬線的檢測(cè)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11

