
.. 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_multicolumn.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_multicolumn.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_subplots_axes_and_figures_gridspec_multicolumn.py:


=============================================
Gridspec for multi-column/row subplot layouts
=============================================

`.GridSpec` is a flexible way to layout
subplot grids.  Here is an example with a 3x3 grid, and
axes spanning all three columns, two columns, and two rows.

.. GENERATED FROM PYTHON SOURCE LINES 11-36

.. code-block:: Python

    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()


.. GENERATED FROM PYTHON SOURCE LINES 37-41

.. tags::

   component: subplot
   level: intermediate


.. _sphx_glr_download_gallery_subplots_axes_and_figures_gridspec_multicolumn.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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