主頁(yè) > 知識(shí)庫(kù) > 解決python繪圖使用subplots出現(xiàn)標(biāo)題重疊的問(wèn)題

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

熱門(mén)標(biāo)簽:語(yǔ)音系統(tǒng) 百度AI接口 硅谷的囚徒呼叫中心 Win7旗艦版 電話運(yùn)營(yíng)中心 企業(yè)做大做強(qiáng) 客戶服務(wù) 呼叫中心市場(chǎng)需求

先上圖

遇到的問(wèn)題

使用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)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • python中使用sys模板和logging模塊獲取行號(hào)和函數(shù)名的方法
  • python繪圖subplots函數(shù)使用模板的示例代碼

標(biāo)簽:山西 海南 長(zhǎng)沙 山西 喀什 安康 濟(jì)南 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解決python繪圖使用subplots出現(xiàn)標(biāo)題重疊的問(wèn)題》,本文關(guān)鍵詞  ;如發(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)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢(xún)

    • 400-1100-266