该方法常用参数的含义如下:
- nrows:表示行数。
- ncols:表示列数。
- figure:表示布局的画布。
- left,bottom,right,top:表示子图的范围。
- wspace:表示子图之间预留的宽度量。
- hspace:表示子图之间预留的高度
1.约束布局:指的是通过一系列限制确定画布的位置,它会先确定元素的一个绝对定位,之后再以这个元素为中心对其它的元素进行定位
2.matplotlib在绘制子图的时候提供了两种约束布局的方法,一.是用subplots()或figure()的constrained_layout 二用的是修改figure. constrained_layout的配置
一.是用subplots()figure()的constrained_layout 的方法创建画布
把constrained_layout 设为True实例代码如下
plt.subplots(constrined_layout=Ture)
1.使用 使用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
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig2 = plt.figure()
spec2 = gridspec.GridSpec(ncols=2, nrows=2, figure=fig2)
f2_axl = fig2.add_subplot(spec2[0,0])
f2_ax2 = fig2.add_subplot(spec2[0,1])
f2_ax3 = fig2.add_subplot(spec2[1,0])
f2_ax4 = fig2.add_subplot(spec2[1,1])
plt.show ()
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure()
spec = gridspec.GridSpec(ncols=2, nrows=2, figure=fig,
height_ratios=[2,3],width_ratios=[2,1])
axl = fig.add_subplot(spec[0,0])
ax2 = fig.add_subplot(spec[0,1])
ax3 = fig.add_subplot(spec[1,0])
ax4 = fig.add_subplot(spec[1,1])
plt.show ()