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

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

.. _sphx_glr_gallery_subplots_axes_and_figures_gridspec_customization.py:


========================================
GridSpec with variable sizes and spacing
========================================

This example demonstrates the use of `.GridSpec` to generate subplots,
the control of the relative sizes of subplots with *width_ratios* and
*height_ratios*, and the control of the spacing around and between subplots
using subplot params (*left*, *right*, *bottom*, *top*, *wspace*, and
*hspace*).

.. redirect-from:: /gallery/userdemo/demo_gridspec03

.. GENERATED FROM PYTHON SOURCE LINES 14-37

.. code-block:: Python


    import matplotlib.pyplot as plt

    from matplotlib.gridspec import GridSpec


    def annotate_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()
    fig.suptitle("Controlling subplot sizes with width_ratios and height_ratios")

    gs = GridSpec(2, 2, width_ratios=[1, 2], height_ratios=[4, 1])
    ax1 = fig.add_subplot(gs[0])
    ax2 = fig.add_subplot(gs[1])
    ax3 = fig.add_subplot(gs[2])
    ax4 = fig.add_subplot(gs[3])

    annotate_axes(fig)


.. GENERATED FROM PYTHON SOURCE LINES 38-55

.. code-block:: Python


    fig = plt.figure()
    fig.suptitle("Controlling spacing around and between subplots")

    gs1 = GridSpec(3, 3, left=0.05, right=0.48, wspace=0.05)
    ax1 = fig.add_subplot(gs1[:-1, :])
    ax2 = fig.add_subplot(gs1[-1, :-1])
    ax3 = fig.add_subplot(gs1[-1, -1])

    gs2 = GridSpec(3, 3, left=0.55, right=0.98, hspace=0.05)
    ax4 = fig.add_subplot(gs2[:, :-1])
    ax5 = fig.add_subplot(gs2[:-1, -1])
    ax6 = fig.add_subplot(gs2[-1, -1])

    annotate_axes(fig)

    plt.show()


.. _sphx_glr_download_gallery_subplots_axes_and_figures_gridspec_customization.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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