网站首页 > 教程文章 正文
调整边界 plt.tight_layout()
遇到上面这种显示不全的问题,可以使用 plt.tight_layout() 方法, 这一行命令
plt.tight_layout()
直接在绘图命令后面就可以,使用后效果如下
- plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
这个函数可以调节pad,w_pad,h_pad三个参数调整axes与figure的位置以及大小关系
多子图subplot
对于多个子图的绘制,可以使用 pyplot 中的 subplot() 和 subplots() 方法。
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rc
rc('text', usetex=True)
plt.rc('font',family='Times New Roman',size=15)
x=[1,2,3,4,5]
y1=[2,3,5,6,8]
y2=[3,4,6,7,9]
fig =plt.figure()
ax1 = fig.add_subplot(2,1,1)
ax2 = fig.add_subplot(2,1,2)
ax1.plot(x,y1,'^-',c='red')
ax1.set_xlabel('xxx')
ax1.set_ylabel('yyy')
ax2.plot(x,y2,'v-',c='blue')
ax2.set_xlabel('xxx')
ax2.set_ylabel('yyy')
plt.show()
- ax1 = fig.add_subplot(2,1,1) 义为 两行,一列,第一列图
- ax2 = fig.add_subplot(2,1,2) 义为 两行,一列,第二列图
- 效果
xxx被挡住了,可以用前面提到的tight_layout()
- 加入一行命令 :plt.tight_layout()
正常显示
- 横排:一行两列
大小不同的图形
fig =plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,3)
ax3 = fig.add_subplot(1,2,2)
ax1.plot(x,y1,'^-',c='red')
ax1.set_xlabel('x1')
ax1.set_ylabel('y1')
ax2.plot(x,y2,'v-',c='blue')
ax2.set_xlabel('x2')
ax2.set_ylabel('y2')
ax3.plot(x,y3,'o-',c='cyan')
ax3.set_xlabel('x3')
ax3.set_ylabel('y3')
plt.tight_layout()
plt.show()
- 效果
- 注释
红色图形占1/4,可以分布在一个两行两列的布局中的第1个:
ax1 = fig.add_subplot(2,2,1)
蓝色图形占1/4,可以分布在一个两行两列的布局中的第3个:
ax3 = fig.add_subplot(1,2,2)
青绿色图形占1/2,可以分布在一个一行两列的布局中的第2个:
ax2 = fig.add_subplot(2,2,3)
inset 子图
- 需要导入库
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
- inset子图绑定一个主图
ax2 = inset_axes(ax1,width=1.3, height=0.9)
这里ax2时inset,ax1是主图
- 代码实例
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rc
rc('text', usetex=True)
plt.rc('font',family='Times New Roman',size=15)
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
x=[1,2,3,4,5]
y1=[2,3,5,6,8]
y2=[3,4,6,7,9]
fig =plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])
ax1.plot(x,y1,'^-',c='red')
ax1.set_xlabel('x1')
ax1.set_ylabel('y1')
ax2 = inset_axes(ax1,width=1.3, height=0.9)
ax2.plot(x,y2,'v-',c='blue')
ax2.set_xlabel('x2')
ax2.set_ylabel('y2')
ax3 = inset_axes(ax1, width="30%", height="40%", loc=4)
ax3.plot(x,y3,'v-',c='orange')
ax3.set_xlabel('x3')
ax3.set_ylabel('y3')
plt.show()
- 效果
猜你喜欢
- 2025-04-05 python3 matplotlib下增加新字体并使用
- 2025-04-05 Matplotlib直方图(matplotlib直方图高度)
- 2025-04-05 Matplotlib饼状图(matplotlib 饼状图)
- 2025-04-05 一图入门matplotlib(matplotlib绘图基础)
- 2025-04-05 【Python】一文学会使用 Matplotlib 库(数据可视化)
- 2025-04-05 Matplotlib Figures的创建、显示和保存
- 2025-04-05 Python技巧之使用Matplotlib绘制数据图表
- 2025-04-05 为什么你觉得Matplotlib用起来困难?因为你还没看过这个思维导图
- 2025-04-05 基于matplotlib轻松绘制漂亮的表格
- 2025-04-05 Matplotlib | 一文搞定Matplotlib从入门到实战演练!
- 最近发表
-
- 网络安全干货知识 | 手把手搭建 k8s docker 漏洞环境
- docker+k8s 报错(k8s docker login)
- K8s 集群运行时:从 Docker 升级到 Containerd
- 轻松掌握k8s安装(使用docker)知识点
- 什么是 k8s(Kubernetes)?Docker 与 Kubernetes选择哪一个?
- 从 Docker 到 K8s:初学者常见的误区盘点
- Docker容器是什么?K8s和它有什么关系呢?
- Docker 是什么? 它与K8S之间是什么关系?
- Docker是什么?K8s是什么?如何从0到1实现Docker与K8s全流程部署
- K8S与Docker的区别(k8s与docker的区别是啥)
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)