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(‘人数’). 注 ...
随机推荐
- 更好的处理 Python 多工程 import 依赖
话说, 这段时间需要开发一个项目, 新项目对现有的几乎所有项目都有依赖. 豆瓣现存的几个大项目,基本都是围绕豆瓣主站shire的依赖, 也就是说, 其他项目对shire的单项依赖, 这些项目在需要主 ...
- Java_hutool 发起请求
//执行接口 String realUrl = "http://localhost:8091/SzeportCodeService/MSGService/encryptAES"; ...
- java实现大文件上传和下载
[文件上传和下载]是很多系统必备功能, 比如PM\OA\ERP等:系统中常见的开发模式有B/S和C/S,而前者主要是通过浏览器来访问web服务器,一般采用七层协议中的[应用层http]进行数据传输,后 ...
- seq2seq聊天模型(一)
原创文章,转载请注明出处 最近完成了sqe2seq聊天模型,磕磕碰碰的遇到不少问题,最终总算是做出来了,并符合自己的预期结果. 本文目的 利用流程图,从理论方面,回顾,总结seq2seq模型, seq ...
- nodejs基础(回调函数、模块、事件、文件读写、目录的创建与删除)
node官网:http://nodejs.cn/ 今天想看看node的视频,对node进一步了解, 1.我们可以从官网下载node到自己的电脑上,今天了解到node的真正概念,node时javascr ...
- 内存管理1 retain & release
内存管理法则 1:谁创建谁释放alloc /new/ copy------>release/autorelease.一一对应,不是你创建的就不用你释放. 2:除了alloc /new/ copy ...
- 六、grep与正则表达式 (文本过滤)
一.正则表达式 正则表达式:Regual Expression, REGEXP.由一类特殊字符及文本字符所编写的模式,其中有些字符不表示其字面意义,而是用于表示控制或通配的功能:基本正则表达式:BRE ...
- 简单的switch插件
页面效果: 这个switch使用纯CSS实现,小巧简单 css代码 /* switch */ /* 开关样式 */ label.bui-switch-label .bui-switch { width ...
- ROS机器人开发实践学习笔记1
刚刚开始学习ROS,打算入机器人的坑了,参考教材是<ROS及其人开发实践>胡春旭编著 机械工业出版社 华章科技出品.本来以为可以按照书上的步骤一步步来,但是,too young to si ...
- Java并发指南8:AQS中的公平锁与非公平锁,Condtion
一行一行源码分析清楚 AbstractQueuedSynchronizer (二) 转自https://www.javadoop.com/post/AbstractQueuedSynchronizer ...