{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Nested Gridspecs\n\nGridSpecs can be nested, so that a subplot from a parent GridSpec can\nset the position for a nested grid of subplots.\n\nNote that the same functionality can be achieved more directly with\n`~.Figure.subfigures`; see\n:doc:`/gallery/subplots_axes_and_figures/subfigures`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n\nimport matplotlib.gridspec as gridspec\n\n\ndef format_axes(fig):\n for i, ax in enumerate(fig.axes):\n ax.text(0.5, 0.5, \"ax%d\" % (i+1), va=\"center\", ha=\"center\")\n ax.tick_params(labelbottom=False, labelleft=False)\n\n\n# gridspec inside gridspec\nfig = plt.figure()\n\ngs0 = gridspec.GridSpec(1, 2, figure=fig)\n\ngs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])\n\nax1 = fig.add_subplot(gs00[:-1, :])\nax2 = fig.add_subplot(gs00[-1, :-1])\nax3 = fig.add_subplot(gs00[-1, -1])\n\n# the following syntax does the same as the GridSpecFromSubplotSpec call above:\ngs01 = gs0[1].subgridspec(3, 3)\n\nax4 = fig.add_subplot(gs01[:, :-1])\nax5 = fig.add_subplot(gs01[:-1, -1])\nax6 = fig.add_subplot(gs01[-1, -1])\n\nplt.suptitle(\"GridSpec Inside GridSpec\")\nformat_axes(fig)\n\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags::\n\n component: subplot\n level: intermediate\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }