我们添加前提条件
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')
效果如下



