1、subplot布局

subplot(nrows, ncols, index, **kwargs)

subplot(pos, **kwargs)

subplot(ax)

  1. x=[1,2,3]
  2. values = [10, 15, 25]
  3. p1=plt.subplot(221)
  4. plt.bar(x, values)
  5. p1.set_ylabel('yy')
  6. p1.set_title('p1')
  7.  
  8. plt.subplot(222)
  9. plt.scatter(x, values)
  10.  
  11. plt.subplot(223)
  12. plt.plot(x, values)
  13. plt.suptitle('subplot')
  14. plt.show()

上面的图第三张只占半个图纸长度,不美观。那么使用subplot怎么画非对阵图呢?重新定义子图的分布行列即可。

plt.subplot(212) 或plt.subplot(2,1,2)把图纸分为2行1列,当前子图是第二个。

  1. x=[1,2,3]
  2. values = [10, 15, 25]
  3. p1=plt.subplot(221)
  4. plt.bar(x, values)
  5. p1.set_ylabel('yy')
  6. p1.set_title('p1')
  7. plt.subplot(222)
  8. plt.scatter(x, values)
  9. plt.subplot(212)
  10. plt.plot(x, values)
  11. plt.suptitle('subplot')
  12. plt.show()

先把图纸分为2行2列,先画图1和图3,然后再把图纸划分为1行2列,对第二列绘图。

  1. x=[1,2,3]
  2. values = [10, 15, 25]
  3. p1=plt.subplot(221)
  4. plt.bar(x, values)
  5. p1.set_ylabel('yy')
  6. p1.set_title('p1')
  7. plt.subplot(223)
  8. plt.scatter(x, values)
  9. plt.subplot(1,2,2)
  10. plt.plot(x, values)
  11. plt.suptitle('subplot')
  12. 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) :创建一个图形和一组子图。

  1. fig, ax = plt.subplots(2, 3)
  2. fig.tight_layout()
  3. ax[0].text(0.5,0.5, 'sss')
  4. 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.

  1. fig = plt.figure()
  2. ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
  3. ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
  4. ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
  5. ax4 = plt.subplot2grid((3, 3), (2, 0))
  6. ax5 = plt.subplot2grid((3, 3), (2, 1))
  7. plt.show()

  1. x = np.arange(1,10)
  2. fig = plt.figure()
  3. ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=1, title = 'plt1')
  4. ax1.plot(x,x*x)
  5. ax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2, title = 'plt2')
  6. ax2.plot(x,x*x)
  7. ax3 = plt.subplot2grid((3, 3), (1, 0), colspan=3, title = 'plt3')
  8. ax3.plot(x,x*x)
  9. ax4 = plt.subplot2grid((3, 3), (2, 0), title = 'plt4')
  10. ax4.plot(x,x*x)
  11. ax5 = plt.subplot2grid((3, 3), (2, 1), title = 'plt5')
  12. ax5.plot(x,x*x)
  13. ax6 = plt.subplot2grid((3, 3), (2, 2), title = 'plt6')
  14. ax6.plot(x,x*x)
  15. plt.legend()
  16. plt.suptitle('subplot2grid figure', x=0.5,y=0.95, ha='center', va='center', fontsize=15)
  17. 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画图总结--多子图布局的更多相关文章

  1. matplotlib 画图

    matplotlib 画图 1. 画曲线图       Tompson = np.array([0, 0, 0, 0, 0.011, 0.051, 0.15, 0.251, 0.35, 0.44, 0 ...

  2. matplotlib画图实例:pyplot、pylab模块及作图參数

    http://blog.csdn.net/pipisorry/article/details/40005163 Matplotlib.pyplot画图实例 {使用pyplot模块} matplotli ...

  3. 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 ...

  4. python matplotlib画图产生的Type 3 fonts字体没有嵌入问题

    ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...

  5. 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令

    使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...

  6. matplotlib画图

    matplotlib画图 import numpy as np import matplotlib.pyplot as plt x1=[20,33,51,79,101,121,132,145,162, ...

  7. python3 使用matplotlib画图出现中文乱码的情况

    python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u’人数’),而是直接写plt.xlabel(‘人数’). 注 ...

  8. python使用matplotlib画图

    python使用matplotlib画图 matplotlib库是python最著名的画图库.它提供了一整套和matlab类似的命令API.十分适合交互式地进行制图. 先介绍了怎样使用matplotl ...

  9. matplotlib画图出现乱码情况

    python3使用matplotlib画图,因python3默认使用中unicode编码,所以在写代码时不再需要写 plt.xlabel(u’人数’),而是直接写plt.xlabel(‘人数’). 注 ...

随机推荐

  1. Docker:18.x版本安装与卸载

    Docker CE 分为 stable, test, 和 nightly 三个更新频道.每六个月发布一个 stable 版本 初始化机器 #1.关闭警报 vi /etc/inputrc #然后将set ...

  2. vue 内联样式style三元表达式(动态绑定样式)

    <span v-bind:style="{'display':config.isHaveSearch ? 'block':'none'}" >动态绑定样式</sp ...

  3. [Luogu] 余数求和

    question: $$\sum_{i=1}^{n} k \bmod i$$$$\sum_{i=1}^{n} k - \lfloor \frac{k}{i} \rfloor i$$$$\sum_{i= ...

  4. HTML基础要点归纳

    一.开发环境 常用的HTML编辑器有Sublime Text.Hbuild.Dreamweare.以及vs code.pycharm等都可以.我目前在用的就是Sublime text3和Hbuild两 ...

  5. P1169 [ZJOI2007]棋盘制作——悬线法

    ---恢复内容开始--- 给你一个矩阵,选出最大的棋盘,棋盘的要求是黑白相间(01不能相邻),求出最大的正方形和矩形棋盘的面积: 数据n,m<=2000; 这个一看就可能是n2DP,但是写不出. ...

  6. Ubuntu14.04 系统复制迁移到新的机器上

    1.打包系统文件 制作启动盘,然后进入bios, #进入bios的boot标签选择sandisk启动 如果没有找到u盘进入save & exit标签页选择boot override中的sand ...

  7. 二十七、Linux内核管理

    内核组成: uname命令 内核:uname,mkinitrd,dracut 模块: lsmod,modinfo,depmod,modprobe,insmod,rmmod /proc,sysctl,/ ...

  8. PyTricks-json dumps优雅的输出字典

    import json my_mapping = {'a': 23, 'b': 42, 'c': 0xc0ffee} print(json.dumps(my_mapping, indent=4, so ...

  9. 《梁宁·产品思维30讲》课程学习笔记(内含全套音频+ppt资料

    科技进步.产品迭代.公司演化.组织变迁……不变的是用户的情绪和人性. 那些信奉“用户驱动”的人,从普通人变成了行业大佬,建立了自己的世界.乔布斯.马化腾.马云.雷军.张小龙.周鸿祎.傅盛……这些改变世 ...

  10. 如何永久激活(破解) IntelliJ IDEA 2018.2.2

    原 如何永久激活(破解) IntelliJ IDEA 2018.2.2 版权声明:本文为博主原创文章,转载不需要博主同意,只需贴上原文链接即可. https://blog.csdn.net/zhige ...