嵌套的Gridspecs

GridSpec可以嵌套,因此来自父GridSpec的子图可以设置嵌套的子图网格的位置。

嵌套的Gridspecs示例

  1. import matplotlib.pyplot as plt
  2. import matplotlib.gridspec as gridspec
  3. def format_axes(fig):
  4. for i, ax in enumerate(fig.axes):
  5. ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
  6. ax.tick_params(labelbottom=False, labelleft=False)
  7. # gridspec inside gridspec
  8. f = plt.figure()
  9. gs0 = gridspec.GridSpec(1, 2, figure=f)
  10. gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])
  11. ax1 = plt.Subplot(f, gs00[:-1, :])
  12. f.add_subplot(ax1)
  13. ax2 = plt.Subplot(f, gs00[-1, :-1])
  14. f.add_subplot(ax2)
  15. ax3 = plt.Subplot(f, gs00[-1, -1])
  16. f.add_subplot(ax3)
  17. gs01 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[1])
  18. ax4 = plt.Subplot(f, gs01[:, :-1])
  19. f.add_subplot(ax4)
  20. ax5 = plt.Subplot(f, gs01[:-1, -1])
  21. f.add_subplot(ax5)
  22. ax6 = plt.Subplot(f, gs01[-1, -1])
  23. f.add_subplot(ax6)
  24. plt.suptitle("GridSpec Inside GridSpec")
  25. format_axes(f)
  26. plt.show()

下载这个示例