先上圖
使用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子圖見(jiàn)的間距
Adjust the padding between and around subplots.
參數(shù):
參考官方文檔:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html#matplotlib.pyplot.tight_layout
部分代碼:
# 顯示圖像 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)文章希望大家以后多多支持腳本之家!
標(biāo)簽:濟(jì)南 宿遷 黃山 喀什 景德鎮(zhèn) 三沙 欽州 臺(tái)灣
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解決python繪圖使用subplots出現(xiàn)標(biāo)題重疊的問(wèn)題》,本文關(guān)鍵詞 解決,python,繪圖,使用,subplots,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。