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

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

.. _sphx_glr_gallery_subplots_axes_and_figures_gridspec_and_subplots.py:


================================================
Combine two subplots using subplots and GridSpec
================================================

Sometimes we want to combine two subplots in an Axes layout created with
`~.Figure.subplots`.  We can get the `~.gridspec.GridSpec` from the Axes
and then remove the covered Axes and fill the gap with a new bigger Axes.
Here we create a layout with the bottom two Axes in the last column combined.

To start with this layout (rather than removing the overlapping Axes) use
`~.pyplot.subplot_mosaic`.

See also :ref:`arranging_axes`.

.. GENERATED FROM PYTHON SOURCE LINES 16-32

.. code-block:: Python


    import matplotlib.pyplot as plt

    fig, axs = plt.subplots(ncols=3, nrows=3)
    gs = axs[1, 2].get_gridspec()
    # remove the underlying Axes
    for ax in axs[1:, -1]:
        ax.remove()
    axbig = fig.add_subplot(gs[1:, -1])
    axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
                   xycoords='axes fraction', va='center')

    fig.tight_layout()

    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 33-37

.. tags::

   component: subplot
   level: beginner


.. _sphx_glr_download_gallery_subplots_axes_and_figures_gridspec_and_subplots.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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