matplotlib-subplot分格显示 发表于 2019-06-11 | 分类于 python 详情请看莫烦老师教程莫烦python 代码1234567891011121314151617181920212223242526272829303132333435363738import matplotlib.pyplot as pltimport matplotlib.gridspec as gridspec# method 1: subplot2grid##########################plt.figure()plt.subplots_adjust(wspace = 0.4, hspace = 0.4) #调整子图间距ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3) # stands for axesax1.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))ax4.scatter([1, 2], [2, 2])ax4.set_xlabel('ax4_x')ax4.set_ylabel('ax4_y')ax5 = plt.subplot2grid((3, 3), (2, 1))# method 2: gridspec#########################plt.figure()plt.subplots_adjust(wspace = 0.4, hspace = 0.4) #调整子图间距gs = gridspec.GridSpec(3, 3)# use index from 0ax6 = plt.subplot(gs[0, :])ax7 = plt.subplot(gs[1, :2])ax8 = plt.subplot(gs[1:, 2])ax9 = plt.subplot(gs[-1, 0])ax10 = plt.subplot(gs[-1, -2])# method 3: easy to define structure####################################f, ((ax11, ax12), (ax13, ax14)) = plt.subplots(2, 2, sharex=True, sharey=True)ax11.scatter([1,2], [1,2])plt.subplots_adjust(wspace = 1, hspace = 0.4) #调整子图间距plt.tight_layout()plt.show() 结果