matplotlib画图总结--多子图布局
1、subplot布局
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)
x=[1,2,3]
values = [10, 15, 25]
p1=plt.subplot(221)
plt.bar(x, values)
p1.set_ylabel('yy')
p1.set_title('p1') plt.subplot(222)
plt.scatter(x, values) plt.subplot(223)
plt.plot(x, values)
plt.suptitle('subplot')
plt.show()

上面的图第三张只占半个图纸长度,不美观。那么使用subplot怎么画非对阵图呢?重新定义子图的分布行列即可。
plt.subplot(212) 或plt.subplot(2,1,2)把图纸分为2行1列,当前子图是第二个。
x=[1,2,3]
values = [10, 15, 25]
p1=plt.subplot(221)
plt.bar(x, values)
p1.set_ylabel('yy')
p1.set_title('p1')
plt.subplot(222)
plt.scatter(x, values)
plt.subplot(212)
plt.plot(x, values)
plt.suptitle('subplot')
plt.show()

先把图纸分为2行2列,先画图1和图3,然后再把图纸划分为1行2列,对第二列绘图。
x=[1,2,3]
values = [10, 15, 25]
p1=plt.subplot(221)
plt.bar(x, values)
p1.set_ylabel('yy')
p1.set_title('p1')
plt.subplot(223)
plt.scatter(x, values)
plt.subplot(1,2,2)
plt.plot(x, values)
plt.suptitle('subplot')
plt.show()

2、subplots布局
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw) :创建一个图形和一组子图。
fig, ax = plt.subplots(2, 3)
fig.tight_layout()
ax[0].text(0.5,0.5, 'sss')
plt.show()

3、subplot2grid布局
matplotlib.pyplot.subplot2grid(shape, loc, rowspan=1, colspan=1, fig=None, **kwargs)
shape : sequence of 2 ints
loc : sequence of 2 ints
rowspan : int
Number of rows for the axis to span to the right.
colspan : int
Number of columns for the axis to span downwards.
fig = plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))
plt.show()

x = np.arange(1,10)
fig = plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=1, title = 'plt1')
ax1.plot(x,x*x)
ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2, title = 'plt2')
ax2.plot(x,x*x)
ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=3, title = 'plt3')
ax3.plot(x,x*x)
ax4 = plt.subplot2grid((3, 3), (2, 0), title = 'plt4')
ax4.plot(x,x*x)
ax5 = plt.subplot2grid((3, 3), (2, 1), title = 'plt5')
ax5.plot(x,x*x)
ax6 = plt.subplot2grid((3, 3), (2, 2), title = 'plt6')
ax6.plot(x,x*x)
plt.legend()
plt.suptitle('subplot2grid figure', x=0.5,y=0.95, ha='center', va='center', fontsize=15)
plt.show()

可见上面的图,x周名称和附件的图互相干涉。需要缩小图或者加大间隙。
使用layout函数plt.tight_layout()。但是图的title和第一行干涉。

plt.tight_layout(rect=[0, 0, 1, 0.95]) 或者添加一句fig.subplots_adjust(top=0.85)即可。
matplotlib.pyplot.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None)
其中rect可以这样定义:[left, bottom, right, top] in normalized (0, 1) figure coordinates,所以可以定义right和top,进行图的缩放。
A rectangle (left, bottom, right, top) in the normalized figure coordinate that the whole subplots area (including labels) will fit into. Default is (0, 0, 1, 1).

图的缩放,做个实验plt.tight_layout(rect=[0, 0, 0.7, 0.5])

matplotlib画图总结--多子图布局的更多相关文章
- matplotlib 画图
matplotlib 画图 1. 画曲线图 Tompson = np.array([0, 0, 0, 0, 0.011, 0.051, 0.15, 0.251, 0.35, 0.44, 0 ...
- matplotlib画图实例:pyplot、pylab模块及作图參数
http://blog.csdn.net/pipisorry/article/details/40005163 Matplotlib.pyplot画图实例 {使用pyplot模块} matplotli ...
- matplotlib画图报错This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
之前用以下代码将实验结果用matplotlib show出来 plt.plot(np.arange(len(aver_reward_list)), aver_reward_list) plt.ylab ...
- python matplotlib画图产生的Type 3 fonts字体没有嵌入问题
ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...
- 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令
使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...
- matplotlib画图
matplotlib画图 import numpy as np import matplotlib.pyplot as plt x1=[20,33,51,79,101,121,132,145,162, ...
- python3 使用matplotlib画图出现中文乱码的情况
python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u’人数’),而是直接写plt.xlabel(‘人数’). 注 ...
- python使用matplotlib画图
python使用matplotlib画图 matplotlib库是python最著名的画图库.它提供了一整套和matlab类似的命令API.十分适合交互式地进行制图. 先介绍了怎样使用matplotl ...
- matplotlib画图出现乱码情况
python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u’人数’),而是直接写plt.xlabel(‘人数’). 注 ...
随机推荐
- Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)
人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...
- [Google Guava] 1.3-常见Object方法
原文链接 译者: 沈义扬 equals 当一个对象中的字段可以为null时,实现Object.equals方法会很痛苦,因为不得不分别对它们进行null检查.使用Objects.equal帮助你执行n ...
- 巧用getdate()测试你的sql执行效率
在开发项目的过程中,我们会遇到各种各样的问题,有时候由于业务逻辑复杂,我们写的sql语句会很长很长,甚至会嵌套很多层,这个时候我就会担心sql执行时间会不会太长了?会不会有什么问题导致执行效率变慢?经 ...
- P3355 骑士共存问题【洛谷】(二分图最大独立集变形题) //链接矩阵存图
展开 题目描述 在一个 n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘上某些方格设置了障碍,骑士不得进入 对于给定的 n*n 个方格的国际象棋棋盘和障碍标志,计算棋盘上最多可 ...
- SQL审核 Inception 中小团队快速构建SQL自动审核系统
SQL审核与执行,作为DBA日常工作中相当重要的一环,一直以来我们都是通过人工的方式来处理,效率低且质量没办法保证.为了规范操作,提高效率,我们决定引入目前市面上非常流行的SQL自动审核工具Incep ...
- 重新梳理一下adb操作app(golang版)
主要参考我之前整理的内容https://www.cnblogs.com/pu369/p/10490668.html,梳理简化一下思路,以便于用最简单的代码来应对无聊人士的要求. 需求主要是:打开手机. ...
- Visual Studio Code:使用技巧汇总
造冰箱的大熊猫@cnblogs 2019/8/9(最后更新2019/8/11) 试用了下微软出的Visual Studio Code,哎呀,有点小惊喜 将VSC的快捷键小结一下,不定期更新 功能 快捷 ...
- 洛谷P1860 新魔法药水
洛谷题目链接 动态规划: 这个题目调了我好久....结果循环变量写错了... 而且题目有个坑!!!只能用开始给你的$v$元买入东西 回归正题: 我们定义状态$ans[i][j]$表示第$i$个物品用了 ...
- 二进制学习——Blob,ArrayBuffer、File、FileReader和FormData的区别
前言: Blob.ArrayBuffer.File.fileReader.formData这些名词总是经常看到,知道一点又好像不知道,像是同一个东西好像又不是,总是模模糊糊,最近终于下决心要弄清楚. ...
- Python互联网金融之用户增长的数据逻辑
怎样看待和应用我们互联网金融中的数据? 怎样进行数据分析? 互联网金融数据分析的三个层面: (1)指标层面 建立指标体系,观察指标涨跌的情况 (2)行业框架 不同的行业对于各个指标的权重不同 复投率 ...