栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Subplot(matplotlib笔记6)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Subplot(matplotlib笔记6)

我们添加前提条件

plt.figure()

plt.show()

我们要使用subplot进行多合一显示,即需如下代码

plt.subplot(2,2,1)
plt.plot([0,1],[0,1])

效果如下

如我们需要把这个figure填满则需要

plt.subplot(2,2,2)
plt.plot([0,1],[0,1])
plt.subplot(2,2,3)
plt.plot([0,1],[0,1])
plt.subplot(2,2,4)
plt.plot([0,1],[0,1])

效果如下

如果我们想把图一占满第一排,则需更改:

plt.subplot(2,1,1)
plt.plot([0,1],[0,1])
plt.subplot(2,3,4)#注意这是4,不是2
plt.plot([0,1],[0,1])
plt.subplot(2,3,5)
plt.plot([0,1],[0,1])
plt.subplot(2,3,6)
plt.plot([0,1],[0,1])


subplot还有还有三种用法
第一种就是subplot2grid

plt.figure()
ax1=plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1)
ax1.plot([1,2],[1,2])
ax1.set_title('ax1_title')

效果如下

我们继续完善,加入如下代码

ax2=plt.subplot2grid((3,3),(1,0),colspan=2,rowspan=1)
ax3=plt.subplot2grid((3,3),(1,2),colspan=1,rowspan=2)
ax4=plt.subplot2grid((3,3),(2,0),colspan=1,rowspan=1)
ax4=plt.subplot2grid((3,3),(2,1),colspan=1,rowspan=1)

效果如下

方法2
我们先要再引入一个模块

import matplot.gridspec as gridspec
plt.figure()
gs=gridspec.GridSpec(3,3)
ax1=plt.subplot(gs[0,:])
ax2=plt.subplot(gs[1,:2])
ax3=plt.subplot(gs[1:,2])
ax4=plt.subplot(gs[-1,0])
ax5=plt.subplot(gs[-1,-2])

效果如下

第三种方法

f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)#这里有s
ax11.scatter([1,2],[1,2])

效果如下

最后就是subplot的图中图

fig=plt.figure()
x=[1,2,3,4,5,6,7]
y=[1,3,4,2,5,8,6]
left,bottom,width,height=0.1,0.1,0.8,0.8
ax1=fig.add_axes([left,bottom,width,height])
ax1.plot(x,y,'r')

left,bottom,width,height=0.2,0.6,0.25,0.25
ax2=fig.add_axes([left,bottom,width,height])
ax2.plot(y,x,'b')

效果如下

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号