matplotlib的gridspec模块是专门指定画布中子图位置的模块,该模块中包含一个GridSpec类,通过创建GridSpec类对象来自定义画布中子图的布局结构,使得子图能够更好地适应画布。
GridSpec类的构造方法的语法格式如下:
GridSpec(nrows, ncols, figure=None, left=None, bottom=None, right=None,
top=None, wspace=None, hspace=None, width_ratios=None, height_ratios=None)
代码及注释如下 :
-
创建画布:
fig = plt.figure()
2.创建 区域规划图
spec = gridspec.GridSpec(3,4, figure=fig)
3.根据给定的“区域规划图”,创建对应的坐标系
ax1 = plt.subplot(spec[0,0:4]) ax2 = plt.subplot(spec[1,0:2]) ax3 = plt.subplot(spec[1,2:4]) ax4 = plt.subplot(spec[2,0:1], projection='polar') ax5 = plt.subplot(spec[2,1:4])
4.调整子图之间的距离
plt.tight_layout()
plt.show()