注意
跳转到末尾 下载完整的示例代码。
Gridspec 用于多列/多行子图布局#
GridSpec
是一种灵活的子图网格布局方式。这里是一个3x3网格的示例,其中轴线跨越所有三列、两列和两行。
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
def format_axes(fig):
for i, ax in enumerate(fig.axes):
ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
ax.tick_params(labelbottom=False, labelleft=False)
fig = plt.figure(layout="constrained")
gs = GridSpec(3, 3, figure=fig)
ax1 = fig.add_subplot(gs[0, :])
# identical to ax1 = plt.subplot(gs.new_subplotspec((0, 0), colspan=3))
ax2 = fig.add_subplot(gs[1, :-1])
ax3 = fig.add_subplot(gs[1:, -1])
ax4 = fig.add_subplot(gs[-1, 0])
ax5 = fig.add_subplot(gs[-1, -2])
fig.suptitle("GridSpec")
format_axes(fig)
plt.show()

脚本总运行时间: (0 分钟 1.060 秒)