
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/subplots_axes_and_figures/align_labels_demo.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_align_labels_demo.py>`
        to download the full example code.

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

.. _sphx_glr_gallery_subplots_axes_and_figures_align_labels_demo.py:


=======================
Align labels and titles
=======================

Aligning xlabel, ylabel, and title using `.Figure.align_xlabels`,
`.Figure.align_ylabels`, and `.Figure.align_titles`.

`.Figure.align_labels` wraps the x and y label functions.

Note that the xlabel "XLabel1 1" would normally be much closer to the
x-axis, "YLabel0 0" would be much closer to the y-axis, and title
"Title0 0" would be much closer to the top of their respective axes.

.. GENERATED FROM PYTHON SOURCE LINES 15-45

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    fig, axs = plt.subplots(2, 2, layout='constrained')

    ax = axs[0][0]
    ax.plot(np.arange(0, 1e6, 1000))
    ax.set_title('Title0 0')
    ax.set_ylabel('YLabel0 0')

    ax = axs[0][1]
    ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
    ax.set_title('Title0 1')
    ax.xaxis.tick_top()
    ax.tick_params(axis='x', rotation=55)


    for i in range(2):
        ax = axs[1][i]
        ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1))
        ax.set_ylabel('YLabel1 %d' % i)
        ax.set_xlabel('XLabel1 %d' % i)
        if i == 0:
            ax.tick_params(axis='x', rotation=55)

    fig.align_labels()  # same as fig.align_xlabels(); fig.align_ylabels()
    fig.align_titles()

    plt.show()


.. GENERATED FROM PYTHON SOURCE LINES 46-52

.. tags::

   component: label
   component: title
   styling: position
   level: beginner


.. _sphx_glr_download_gallery_subplots_axes_and_figures_align_labels_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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