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(‘人数’). 注 ...
随机推荐
- BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )
题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...
- E:only-child
E:only-child 语法: E:only-child { sRules } 说明: 匹配父元素仅有的一个子元素E.大理石机械构件维修 要使该属性生效,E元素必须是某个元素的子元素,E的父元素最高 ...
- php MySQL 查询数据
以下为在MySQL数据库中查询数据通用的 SELECT 语法: SELECT column_name,column_name FROM table_name [WHERE Clause] [LIMIT ...
- fastJson与jackson性能对比
转载:https://blog.csdn.net/u013433821/article/details/82905222最近项目用到fastJson和jackson,为了决定到底弃用哪个,随手写了个测 ...
- 基于Web的文件上传管理系统
一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不能这样上传了.我这里分享一下我自己开发的一套大文件上传控件 ...
- [Luogu] 线段树 2
https://www.luogu.org/problemnew/show/P3373 双懒标记下放 先乘后加 #include <bits/stdc++.h> using namespa ...
- linux mysql5.7设置中文字符集
ubuntu16.04已测试 注意版本,好像从5.6根5.5就不一样,配置文件更深了一层. 1.用vim或nano编辑 /etc/mysql/mysql.conf.d/mysqld.cnf 2.[ ...
- fiddler在小米8下抓取https数据包.
问题,在小米8下一直报 证书链问题,爬了半天帖子发现可能是Android版本问题,有的说用Charles没问题. 没有测试,网上接着爬帖子... 稍稍说下导入证书的问题吧. 可以使用浏览器下载证书,也 ...
- vue中mixin的理解与用法
vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用.最开始我一度认为这个和组件好像没啥区别..后来发现错了.下面我们来看看mixins和普通情况下引入组件有什么区别? 组件在引 ...
- springboot实现异步调用
介绍 所谓的异步执行其实就是使用多线程的方式实现异步调用. 异步有什么好处呢? 如果一个业务逻辑执行完成需要多个步骤,也就是调用多个方法去执行, 这个时候异步执行比同步执行相应更快.不过要注意异步请求 ...