Python subplots函数绘图中标题重叠怎么办
Admin 2022-06-13 群英技术资讯 599 次浏览
先上图
使用plt.subplots(2,2)绘图时,子图的标题和上图重叠,影响观感:
源代码:
import numpy as np from scipy import signal from skimage import data from matplotlib import pyplot as plt # 定义二维灰度图像的空间滤波函数 def correl2d(img, window): # 使用滤波器实现图像的空间相关 # mode = 'same'表示输出尺寸等于输入尺寸 # boundary = 'fill'表示滤波前,用常量值填充原始图像的边缘,默认常量值为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) # 生成滤波结果 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()
函数原型:
matplotlib.pyplot.tight_layout(*, pad=1.08, h_pad=None, w_pad=None, rect=None)
作用:调整subplots子图见的间距
Adjust the padding between and around subplots.
参数:
参考官方文档: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测试结果:
方法2:在subplots中设置figsize
fig, axs = plt.subplots(2, 2,figsize=(6, 15))
# 显示图像 plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文 # 设置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测试结果:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
CGI 程序可以是 Python 脚本,PERL 脚本,SHELL 脚本,C 或者 C++ 程序等。CGI 目前由 NCSA 维护,NCSA 定义 CGI 如下: CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户端 HTML 页面的接口。
这篇文章主要介绍了Python如何读取16进制byte数据,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要介绍了python基于Pandas读写MySQL数据库,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
Linux/Unix 的系统上,Python 解释器通常被安装在 /usr/local/python3 这样的有效路径(目录)里。
本篇文章给大家带来了关于Python的相关知识,其中主要整理了随机森林模型的相关问题,包括了集成模型简介、随机森林模型基本原理、使用sklearn实现随机森林模型等等内容,
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008