Matplotlib解决多子图时的间距问题_matplotlib 子图间隔-程序员宅基地

技术标签: python  matplotlib  Python  

在Matplotlib中,经常使用子图功能,在同一个界面中并排显示多幅图。然而,使用默认设置的话,这些子图往往会相互重叠。

Often you may use subplots to display multiple plots alongside each
other in Matplotlib. Unfortunately, these subplots tend to overlap
each other by default.


解决这个问题的最简单方法是使用Matplotlibtight_layout()函数。本教程解释如何在实践中使用这个函数。

The easiest way to resolve this issue is by using the Matplotlib
tight_layout() function. This tutorial explains how to use this
function in practice.


创建子图

Create Subplots


假设我们需要2列2行,共4个子图:

Consider the following arrangement of 4 subplots in 2 columns and 2
rows:

import matplotlib.pyplot as plt

#define subplots
fig, ax = plt.subplots(2, 2)

#display subplots 
plt.show()

在这里插入图片描述
可以看出这些子图有一点重叠。

Notice how the subplots overlap each other a bit.


使用tight_layout()调整子图间距

Adjust Spacing of Subplots Using tight_layout()


要想解决子图重叠问题,最简单的办法就是使用tight_layout()函数:

The easiest way to resolve this overlapping issue is by using the
Matplotlib tight_layout() function:


import matplotlib.pyplot as plt

#define subplots
fig, ax = plt.subplots(2, 2)
fig.tight_layout()

#display subplots 
plt.show()

在这里插入图片描述

调整子图标题间距

Adjust Spacing of Subplot Titles


当我们给每个子图都添加标题之后,即使是使用了tight_layout()也没法阻止重叠问题的出现:

In some cases you may also have titles for each of your subplots.
Unfortunately even the tight_layout() function tends to cause the
subplot titles to overlap:


import matplotlib.pyplot as plt

#define subplots
fig, ax = plt.subplots(2, 2)
fig.tight_layout()

#define subplot titles
ax[0, 0].set_title('First Subplot')
ax[0, 1].set_title('Second Subplot')
ax[1, 0].set_title('Third Subplot')
ax[1, 1].set_title('Fourth Subplot')

#display subplots 
plt.show()

在这里插入图片描述
解决这个问题的方法是使用h_pad参数来增加子图间距:

The way to resolve this issue is by increasing the height padding
between subplots using the h_pad argument:


import matplotlib.pyplot as plt

#define subplots
fig, ax = plt.subplots(2, 2)
fig.tight_layout(h_pad=2)

#define subplot titles
ax[0, 0].set_title('First Subplot')
ax[0, 1].set_title('Second Subplot')
ax[1, 0].set_title('Third Subplot')
ax[1, 1].set_title('Fourth Subplot')

#display subplots 
plt.show()

在这里插入图片描述

调整总标题的间距

Adjust Spacing of Overall Title


如果想要避免总标题和子图标题重叠,subplots_adjust()函数是个不错的选择:

If you have an overall title, you can use the subplots_adjust()
function to ensure that it doesn’t overlap with the subplot titles:


import matplotlib.pyplot as plt

#define subplots
fig, ax = plt.subplots(2, 2)
fig.tight_layout(h_pad=2)

#define subplot titles
ax[0, 0].set_title('First Subplot')
ax[0, 1].set_title('Second Subplot')
ax[1, 0].set_title('Third Subplot')
ax[1, 1].set_title('Fourth Subplot')

#add overall title and adjust it so that it doesn't overlap with subplot titles
fig.suptitle('Overall Title')
plt.subplots_adjust(top=0.85)

#display subplots 
plt.show()

在这里插入图片描述
点击这里探索更多Matplotlib教程

You can find more Matplotlib tutorials here.


参考链接

How to Adjust Spacing Between Matplotlib Subplots.

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39591031/article/details/119902373

智能推荐

ModuleNotFoundError: No module named ‘_ctypes‘的解决办法_modulenotfounderror: no module named '_ctypes-程序员宅基地

文章浏览阅读8.2k次,点赞7次,收藏12次。问题描述在linux上安装好python3.8.2后,要安装一个第三方依赖库,在安装此依赖库的时候报了ModuleNotFoundError: No module named '_ctypes'的error,导致安装失败,字面上理解就是缺少_ctypes这个module执行命令 yum install libffi-devel进行安装然后从"./configure …"重新安装python3.8.2所有前置依赖安装yum -y install zlib-devel bzip2-devel open_modulenotfounderror: no module named '_ctypes

PyQt5 界面展示图片问题(QtGui.QPixmap)-程序员宅基地

文章浏览阅读2.4w次,点赞18次,收藏97次。PyQt5问题总结之QPixmap界面展示图片有两种方式:1,通过图片的地址路径加载图片并显示example:from PyQt5.Qt import QtGui# 图片路径img_path="image_path.jpg"# 设置展示..._qtgui.qpixmap

mysql LAST_INSERT_ID() 为空值的原因_oceanbase插入id为空-程序员宅基地

文章浏览阅读690次。1.插入的id不为mysql自增主键,则无法查询到mysql官方文档中说道,LAST_INSERT_ID()会获取最后一个生成的AUTO_INCREMENT的值With no argument, LAST_INSERT_ID() returns a BIGINT UNSIGNED (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT _oceanbase插入id为空

CTF-密码学相关_密钥eyo-程序员宅基地

文章浏览阅读5.8k次,点赞7次,收藏41次。参考:千千秀字、百度百科、CTF编码和加密总结、CTF常见编码和加密特征 、CTF中Crypty(密码类)入门必看目录字符编码1.ASCII编码2.Unicode编码3.UTF-8编码4.UTF-16编码5.进制转换6.URL字符编码7.摩斯电码8.Base64/32/16编码9.shellcode编码10.Quoted-printable编码..._密钥eyo

呦西震惊--datatables如何拿到关联对象中的值_data, type, row, meta 获取对象-程序员宅基地

文章浏览阅读266次。例如在json数据中user[{‘name’:‘zhangsan’,‘sex’:‘男’},class[{‘classname’:‘ruanjian’}]]要想拿到user关联的class的数据,先看看一下参考:function render( data, type, row, meta )描述:如果做为一个function,那么每当Datatables从columns中的cell获取数据时,都需要执行该方法。 注意,该方法会被多次调用,根据不同的数据类型,比如sorting(排序)、filter_data, type, row, meta 获取对象

前端学习笔记——XHTML及HTML5新变化_vs中doctype:xhtml5是什么意思-程序员宅基地

文章浏览阅读310次。一、XHTML介绍1.XHTML简介  (1)XHTML是可扩展的超文本标记语言  (2)三种XHTML文档类型    STRICT(严格类型)    TRANSITIONAL(过渡类型)    FRAMESET(框架类型)2.XHTML元素  XHTML元素必须正确正确嵌套、始终关闭、小写、有一个根元素3.XHTML属性  XHTML属性必须小写、用引号包围、最小化是禁止的..._vs中doctype:xhtml5是什么意思

随便推点

spark面试题总结-程序员宅基地

文章浏览阅读7.5k次,点赞5次,收藏77次。spark面试基础篇_spark面试题

实验7-1-13 装箱问题 (20 分)_-20 装箱问题 分数 20 作者 ds课程组 单位 浙江大学 假设有n项物品,大小分别为s 1-程序员宅基地

文章浏览阅读242次。实验7-1-13 装箱问题 (20 分)假设有N项物品,大小分别为s ​1 ​​ 、s ​2 ​​ 、…、s ​i ​​ 、…、s ​N ​​ ,其中s ​i ​​ 为满足1≤s​i ​​ ≤100的整数。要把这些物品装入到容量为100的一批箱子(序号1-N)中。装箱方法是:对每项物品,顺序扫描箱子,把该物品放入足以能够容下它的第一个箱子中。请写一个程序模拟这种装箱过程,并输出每个物品所在的箱子序号,以及放置全部物品所需的箱子数目。输入格式: 输入第一行给出物品个数N(≤1000);第二行给出_-20 装箱问题 分数 20 作者 ds课程组 单位 浙江大学 假设有n项物品,大小分别为s 1

一些重要的C和C++开发框架和开源工具_用c和c++写的开源工具-程序员宅基地

文章浏览阅读2.9k次。1、值得学习的C语言开源项目1.1. WebbenchWebbench是一个在linux下使用的非常简单的网站压测工具。它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连接去测试网站的负载能力。Webbench使用C语言编写, 代码实在太简洁,源码加起来不到600行。 下载链接:http://home.tiscali.cz/~cz2105_用c和c++写的开源工具

18.10.29 POJ 3691 DNA repair(AC自动机+dp)-程序员宅基地

文章浏览阅读99次。描述Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing cha...

查找 —— 静态查找法(顺序查找法、折半查找法、分块查找法)_折半查找不成功平均查找长度-程序员宅基地

文章浏览阅读1.1k次。一、顺序查找法算法思想:依次与每个关键字逐个比较,如果与给定值相等,则查找成功,返回成功值;如果与所有关键字都不相等,则查找失败,返回失败值。其平均查找长度是(n+1)/2实现:int Search(int R[],int n,int k) // 在长度为n的R[]中查找数值为k的元素{ int i; for(i=0;i<n;i++) { if(a[i]==k) r..._折半查找不成功平均查找长度

【六十二】【算法分析与设计】买苹果_牛客题霸_牛客网,牛牛爱博弈,829. 连续整数求和,对数器找规律法,博弈论2^k移动对3取余规律,取余的性质整除性-程序员宅基地

文章浏览阅读901次,点赞25次,收藏29次。小易去附近的商店买苹果,奸诈的商贩使用了捆绑交易,只提供6个每袋和8个每袋的包装(包装不可拆分)。可是小易现在只想购买恰好n个苹果,小易想购买尽量少的袋数方便携带。如果不能购买恰好n个苹果,小易将不会购买。输入一个整数n,表示小易想购买n(1 ≤ n ≤ 100)个苹果输出一个整数表示最少需要购买的袋数,如果不能买恰好n个苹果则输出-1输入:20复制输出:3复制。

推荐文章

热门文章

相关标签