解決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測試結(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測試結(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Django實(shí)現(xiàn)個(gè)人博客系統(tǒng)的搭建
個(gè)人博客是一個(gè)非常好的平臺(tái),可以讓人們分享自己的知識(shí)和經(jīng)驗(yàn),也可以讓人們交流和互動(dòng)。在這篇文章中,我們將介紹如何使用Python Django框架來開發(fā)一個(gè)個(gè)人博客系統(tǒng),希望對(duì)大家有所幫助2023-04-04
關(guān)于pygame.surface.blit()方法4個(gè)參數(shù)的使用
這篇文章主要介紹了關(guān)于pygame.surface.blit()方法4個(gè)參數(shù)的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
昨晚我用python幫隔壁小姐姐P證件照然后發(fā)現(xiàn)
大家好,我是Lex 喜歡欺負(fù)超人那個(gè)Lex 建議大家收藏哦,以后幫小姐姐P自拍,證件照,調(diào)尺寸,背景,摳圖,直接10行代碼搞定,瞬間高大上2021-08-08
python 使用get_argument獲取url query參數(shù)
這篇文章主要介紹了python 使用get_argument獲取url query參數(shù)的相關(guān)資料,需要的朋友可以參考下2017-04-04
解決pip安裝的第三方包在PyCharm無法導(dǎo)入的問題
這篇文章主要介紹了關(guān)于pip安裝的第三方包在PyCharm無法導(dǎo)入的問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
python實(shí)現(xiàn)連連看輔助(圖像識(shí)別)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)連連看輔助程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
python PyAutoGUI 模擬鼠標(biāo)鍵盤操作和截屏功能
一款跨平臺(tái)/無依賴的自動(dòng)化測試工具,目測只能控制鼠標(biāo)/鍵盤/獲取屏幕尺寸/彈出消息框/截屏。這篇文章主要介紹了python PyAutoGUI 模擬鼠標(biāo)鍵盤操作和截屏功能,需要的朋友可以參考下2019-08-08
計(jì)算python腳本執(zhí)行時(shí)間的多種方法
在編寫Python腳本時(shí),了解腳本的執(zhí)行時(shí)間通常是很有用的,特別是在優(yōu)化代碼或評(píng)估性能時(shí),Python提供了多種方法來測量腳本的執(zhí)行時(shí)間,從內(nèi)置模塊到第三方庫,可以選擇適合你需求的方式,本文將介紹計(jì)算 Python 腳本執(zhí)行時(shí)間的多種方法,需要的朋友可以參考下2023-11-11

