matplotlib 进阶之Tight Layout guide
文章目录
- 简单的例子
- Use with GridSpec
- Legend and Annotations
- Use with AxesGrid1
- Colorbar
- 函数链接
matplotlib教程学习笔记
如何使用tight_layout?
tight_layout作用于ticklabels, axis, labels, titles等Artist
简单的例子
import matplotlib.pyplot as plt
import numpy as np
下面的例子和constrained_layout中的是一样的,notebook没有显示出其中的问题,就是labels被遮挡了
plt.rcParams['savefig.facecolor'] = "0.8"
def example_plot(ax, fontsize=12):ax.plot([1, 2])ax.locator_params(nbins=3)ax.set_xlabel('x-label', fontsize=fontsize)ax.set_ylabel('y-label', fontsize=fontsize)ax.set_title('Title', fontsize=fontsize)plt.close('all')
fig, ax = plt.subplots()
example_plot(ax, fontsize=24)
fig, ax = plt.subplots()
example_plot(ax, fontsize=24)
plt.tight_layout()
注意到,每次作图,我们都需要通过使用plt.tight_layout()函数来激活,我们也可以通过
fig.set_tight_layout(True)使得每次作图都会自动tight布局,当然,还可以通过将
figure.autolayout rcParam设置为True来实现。
有多个plots的时候,会出现重叠的现象,通过tight_layout可以解决
plt.close('all')fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
plt.tight_layout()
tight_layout可以通过参数pad, w_pad, h_pad来设置一些布局的细节
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)
example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=2)
即使subplots的大小不一致,tight_layout依旧能够工作
plt.close('all')
fig = plt.figure()ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)example_plot(ax1)
example_plot(ax2)
example_plot(ax3)plt.tight_layout()
对subplot2grid也有效,注意subplot2grid参数为:
shape: e.g. (3, 3) 表示 3 × 3 3 \times 3 3×3个格子
loc: e.g. (0, 1) 表示从第一行第二列个格子开始
rowspan: 跨行
colspan: 跨列
plt.close('all')
fig = plt.figure()ax1 = plt.subplot2grid((3, 3), (0, 0))
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)
ax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)example_plot(ax1)
example_plot(ax2)
example_plot(ax3)
example_plot(ax4)plt.tight_layout()
arr = np.arange(100).reshape((10, 10))plt.close('all')
fig = plt.figure(figsize=(5, 4))ax = plt.subplot(111)
im = ax.imshow(arr, interpolation="none")plt.tight_layout()
Use with GridSpec
Gridspec 拥有自己的tight_layout()方法, 当然,plt.tight_layout也是有效的
import matplotlib.gridspec as gridspecplt.close('all')
fig = plt.figure()gs1 = gridspec.GridSpec(2, 1)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])example_plot(ax1)
example_plot(ax2)gs1.tight_layout(fig)
gs.tight_layout提供rect参数,表示一个外界的框框
默认是(0, 0, 1, 1)
(x1, y1, x2, y2)
(x1, y1)矩形限制框左下角点
(x2, y2)矩形限制框右上角点
fig = plt.figure()gs1 = gridspec.GridSpec(2, 1)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])example_plot(ax1)
example_plot(ax2)gs1.tight_layout(fig, rect=[0, 0, 0.5, 1])
这个功能可以很好的用在分割图形,以及分块操作上
fig = plt.figure()gs1 = gridspec.GridSpec(2, 1)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])example_plot(ax1)
example_plot(ax2)gs1.tight_layout(fig, rect=[0, 0, 0.5, 1])gs2 = gridspec.GridSpec(3, 1)for ss in gs2:ax = fig.add_subplot(ss)example_plot(ax)ax.set_title("")ax.set_xlabel("")ax.set_xlabel("x-label", fontsize=12)gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)# We may try to match the top and bottom of two grids ::
#为了让俩块图形上下一致,需要进行下面的操作
top = min(gs1.top, gs2.top)
bottom = max(gs1.bottom, gs2.bottom)gs1.update(top=top, bottom=bottom)
gs2.update(top=top, bottom=bottom)
plt.show()
但是呢,Title和右边的边边不齐,所以框框是不包含title的?
fig = plt.gcf()gs1 = gridspec.GridSpec(2, 1)
ax1 = fig.add_subplot(gs1[0])
ax2 = fig.add_subplot(gs1[1])example_plot(ax1)
example_plot(ax2)gs1.tight_layout(fig, rect=[0, 0, 0.5, 1])gs2 = gridspec.GridSpec(3, 1)for ss in gs2:ax = fig.add_subplot(ss)example_plot(ax)ax.set_title("")ax.set_xlabel("")ax.set_xlabel("x-label", fontsize=12)gs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)top = min(gs1.top, gs2.top)
bottom = max(gs1.bottom, gs2.bottom)gs1.update(top=top, bottom=bottom)
gs2.update(top=top, bottom=bottom)top = min(gs1.top, gs2.top)
bottom = max(gs1.bottom, gs2.bottom)gs1.tight_layout(fig, rect=[None, 0 + (bottom-gs1.bottom),0.5, 1 - (gs1.top-top)])
gs2.tight_layout(fig, rect=[0.5, 0 + (bottom-gs2.bottom),None, 1 - (gs2.top-top)],h_pad=0.5)
Legend and Annotations
fig, ax = plt.subplots(figsize=(4, 3))
lines = ax.plot(range(10), label='A simple plot')
ax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',)
fig.tight_layout()
plt.show()
有些时候,我们不希望legend也在tight_layout的掌控范围之内,这个时候,我们可以设置leg.set_in_layout(False)
fig, ax = plt.subplots(figsize=(4, 3))
lines = ax.plot(range(10), label='B simple plot')
leg = ax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',)
leg.set_in_layout(False)
fig.tight_layout()
plt.show()
Use with AxesGrid1
没看懂
from mpl_toolkits.axes_grid1 import Gridplt.close('all')
fig = plt.figure()
grid = Grid(fig, rect=111, nrows_ncols=(2, 2),axes_pad=0.25, label_mode='L',)for ax in grid:example_plot(ax)
ax.title.set_visible(False)plt.tight_layout()
Colorbar
plt.close('all')
arr = np.arange(100).reshape((10, 10))
fig = plt.figure(figsize=(4, 4))
im = plt.imshow(arr, interpolation="none")plt.colorbar(im, use_gridspec=True)plt.close('all')
arr = np.arange(100).reshape((10, 10))
fig = plt.figure(figsize=(4, 4))
im = plt.imshow(arr, interpolation="none")plt.colorbar(im, use_gridspec=True)plt.tight_layout()
from mpl_toolkits.axes_grid1 import make_axes_locatableplt.close('all')
arr = np.arange(100).reshape((10, 10))
fig = plt.figure(figsize=(4, 4))
im = plt.imshow(arr, interpolation="none")divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("right", "5%", pad="3%")
plt.colorbar(im, cax=cax)plt.tight_layout()
函数链接
plt.tight_layout
matplotlib 进阶之Tight Layout guide相关推荐
- xx.xib: error: Illegal Configuration: Safe Area Layout Guide before iOS 9.0报错问题解决
之前是用xcode8.3.3创建的工程最近升级到Xcode9.0 遇见了这个问题 在Xcode 9.0以上 新建xib文件会报错 xx.xib: error: Illegal Configuratio ...
- autolayout中 top layout guide详解
Top Layout Guide用于自动布局的辅助,在Storyboard中可以看到Top Layout Guide作为ViewController的属性存在,也就是topLayoutGuide,官方 ...
- 大数据分析——Matplotlib进阶教程
文章目录 问题区设置坐标轴 1.matplotlib.pyplot总览 (1)总函数 (2)常用函数 常用函数解析 对于figure和axes的理解 2.实战 (1)三维图 3D画图常用函数 np.m ...
- Log4j2进阶使用(Pattern Layout详细设置)
1.进阶说明 通过配置Layout打印格式化的日志, Log4j2支持很多的Layouts: CSV GELF HTML JSON Pattern Serialized Syslog XML YAML ...
- Matplotlib进阶教程:布局讲解
在后台回复[阅读书籍] 即可获取python相关电子书~ Hi,我是山月. 今天来给大家介绍下Matplotlib的布局部分~ 01 自定义图形布局 可以创建axes的网格状组合的方法: 1)subp ...
- Matplotlib进阶:利用rcParams控制图形属性
目录 概要 What is rc setting? What is rcParams? matplotlibrc文件在哪儿 缺省设置的绘图例 利用rcParams修改设置属性 小结 概要 本文简单介绍 ...
- iOS---------- Safe Area Layout Guide before iOS 9.0
如果你们的项目不做iOS9以下支持就打开main.storyboard 去除Use safe Area Layout 如果不考虑iOS9以下支持就按照下面的步骤 选中控制器,右边面板的Build ...
- matplotlib 进阶之Artist tutorial(如何操作Atrist和定制)
目录 基本 plt.figure() fig.add_axes() ax.lines set_xlabel 一个完整的例子 定制你的对象 obj.set(alpha=0.5, zorder=2), o ...
- Matplotlib进阶教程:颜色讲解
在后台回复[阅读书籍] 即可获取python相关电子书~ Hi,我是山月. 今天来给大家介绍下Matplotlib里的颜色部分. 01 指定颜色 Matplotlib可以通过以下格式来指定颜色: 一个 ...
最新文章
- 直观讲解一下RPC调用和HTTP调用的区别
- Java 编程的动态性,第 7 部分: 用 BCEL 设计字节码--转载
- python最长连续子串_LeetCode 03无重复字符的最长子串(滑动窗口)
- aws 服务器之间文件转发,aws bucket之间相互拷贝数据
- 单链表的快速排序(转)
- G1与CMS的区别是什么
- Vmware15的安装(ps解决:重装Vmware出现无法安装服务Vmware Authorization Service)
- P3181-[HAOI2016]找相同字符【SAM】
- centos7编译 openjdk8
- 北斗三号b1c频点带宽_北斗三号导航信号的创新设计(一)
- java中operationBox_Java使用PDFBox开发包实现对PDF文档内容编辑与保存
- mysql uncompress_如何在php中实现mysql compress()函数
- jquery ajax xml attribute,获得jQuery ajax和asp.net webmethod xml响应工作
- 单元测试的编写(asp.net) (VS2017)
- 销售订单获取不到即时库存
- 收据找不到怎么退押金_押金收据单不见了,能退押金吗,合同上有写押金多少的 - 找法网免费法律咨询...
- 初接触RTMP流媒体实时消息传输协议
- BZOJ2959长跑——LCT+并查集(LCT动态维护边双连通分量)
- 微信生态圈的发展分析
- python从入门到精通编程汪老师_游戏AI开发从入门到精通:最全游戏AI编程书单...