CODE1:
import matplotlib.pyplot as plt
plt.figure()
plt.subplot(2,2,1)
plt.plot([0,1],[0,1])
plt.subplot(2,2,2)
plt.plot([0,1],[0,2])
plt.subplot(2,2,3)
plt.plot([0,1],[0,3])
plt.subplot(2,2,4)
plt.plot([0,1],[0,4])
plt.show()
RESULT:
CODE2:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
# method 1:subplot2grid
#########################
plt.figure()
ax1=plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1) #(3,3)整个grid三行三列,ax1起始(0,0),列跨度3,行跨度1
ax1.plot([1,2],[1,2])
ax1.set_title('ax1_title')
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))
#method 2:gridspec
#########################
plt.figure()
gs=gridspec.GridSpec(3,3) # 三行三列
ax1=plt.subplot(gs[0,:]) # 第一行全占
ax1.set_title('ax1')
ax2=plt.subplot(gs[1,:2]) #第2行,占0,1两列
ax2.set_title('ax2')
ax3=plt.subplot(gs[1:,2]) #第1,2行占,第2列
ax3.set_title('ax3')
ax4=plt.subplot(gs[-1,0]) # 负数表示倒数第几,-1表示倒数第1个
ax4.set_title('ax4')
ax5=plt.subplot(gs[-1,-2]) # 表示最后1行第2列
ax5.set_title('ax5')
# method 3:easy to define structure
###############################################
f,((ax11,ax12),(ax21,ax22))=plt.subplots(2,2,sharex=True,sharey=True)
ax11.scatter([1,2],[1,2])
plt.tight_layout()
plt.show()
RESULT: