
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/subplots_axes_and_figures/gridspec_nested.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. meta::
        :keywords: codex

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_gallery_subplots_axes_and_figures_gridspec_nested.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_subplots_axes_and_figures_gridspec_nested.py:


================
Nested Gridspecs
================

GridSpecs can be nested, so that a subplot from a parent GridSpec can
set the position for a nested grid of subplots.

Note that the same functionality can be achieved more directly with
`~.Figure.subfigures`; see
:doc:`/gallery/subplots_axes_and_figures/subfigures`.

.. GENERATED FROM PYTHON SOURCE LINES 14-48

.. code-block:: Python

    import matplotlib.pyplot as plt

    import matplotlib.gridspec as 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)


    # gridspec inside gridspec
    fig = plt.figure()

    gs0 = gridspec.GridSpec(1, 2, figure=fig)

    gs00 = gridspec.GridSpecFromSubplotSpec(3, 3, subplot_spec=gs0[0])

    ax1 = fig.add_subplot(gs00[:-1, :])
    ax2 = fig.add_subplot(gs00[-1, :-1])
    ax3 = fig.add_subplot(gs00[-1, -1])

    # the following syntax does the same as the GridSpecFromSubplotSpec call above:
    gs01 = gs0[1].subgridspec(3, 3)

    ax4 = fig.add_subplot(gs01[:, :-1])
    ax5 = fig.add_subplot(gs01[:-1, -1])
    ax6 = fig.add_subplot(gs01[-1, -1])

    plt.suptitle("GridSpec Inside GridSpec")
    format_axes(fig)

    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 49-53

.. tags::

   component: subplot
   level: intermediate


.. _sphx_glr_download_gallery_subplots_axes_and_figures_gridspec_nested.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: gridspec_nested.ipynb <gridspec_nested.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: gridspec_nested.py <gridspec_nested.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: gridspec_nested.zip <gridspec_nested.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
