import matplotlib.pyplot as plt plt.figure() """ 创建第1个小图, (3,3)表示将整个图像窗口分成3行3列, (0,0)表示从第0行第0列开始作图,colspan=3表示此小图跨3列, rowspan=1表示行的跨度为1. colspan和rowspan默认跨度为1且为缺省值. """ ax1 = plt.subplot2grid((3,3),(0,0),colspan=3) ax1.plot([1,2], [1,2]) # 第一个小图两个点连线,x为1和1,y与之对应的为2和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)) ax4.scatter([1,2], [2,2]) ax4.set_xlabel('ax4_x') ax4.set_ylabel('ax4_y') plt.show()
import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec plt.figure() gs = gridspec.GridSpec(3, 3) #将窗口分为三行三列 ax6 = plt.subplot(gs[0, :])#第一个小图占据第一行的所有列 ax7 = plt.subplot(gs[1, :2]) ax8 = plt.subplot(gs[1:, 2]) ax9 = plt.subplot(gs[-1, 0])#-1代表倒数第一行 ax10 = plt.subplot(gs[-1, -2]) plt.show()
import matplotlib.pyplot as plt plt.figure() """ 建立一个2行2列的图像窗口,sharex=True表示共享x轴坐标, sharey=True表示共享y轴坐标. ((ax11, ax12), (ax13, ax14))表示第1行从左至右依次放ax11和ax12, 第2行从左至右依次放ax13和ax14. """ f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True) ax11.scatter([1,2], [1,2])#散点图,两个点(1,1),(2,2) plt.tight_layout() #图像紧凑 plt.show()