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

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

.. _sphx_glr_gallery_text_labels_and_annotations_align_ylabels.py:


==============
Align y-labels
==============

Two methods are shown here, one using a short call to `.Figure.align_ylabels`
and the second a manual way to align the labels.

.. redirect-from:: /gallery/pyplots/align_ylabels

.. GENERATED FROM PYTHON SOURCE LINES 11-50

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np


    def make_plot(axs):
        box = dict(facecolor='yellow', pad=5, alpha=0.2)

        # Fixing random state for reproducibility
        np.random.seed(19680801)
        ax1 = axs[0, 0]
        ax1.plot(2000*np.random.rand(10))
        ax1.set_title('ylabels not aligned')
        ax1.set_ylabel('misaligned 1', bbox=box)
        ax1.set_ylim(0, 2000)

        ax3 = axs[1, 0]
        ax3.set_ylabel('misaligned 2', bbox=box)
        ax3.plot(np.random.rand(10))

        ax2 = axs[0, 1]
        ax2.set_title('ylabels aligned')
        ax2.plot(2000*np.random.rand(10))
        ax2.set_ylabel('aligned 1', bbox=box)
        ax2.set_ylim(0, 2000)

        ax4 = axs[1, 1]
        ax4.plot(np.random.rand(10))
        ax4.set_ylabel('aligned 2', bbox=box)


    # Plot 1:
    fig, axs = plt.subplots(2, 2)
    fig.subplots_adjust(left=0.2, wspace=0.6)
    make_plot(axs)

    # just align the last column of Axes:
    fig.align_ylabels(axs[:, 1])
    plt.show()




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_align_ylabels_001.png
   :alt: ylabels not aligned, ylabels aligned
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_align_ylabels_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_align_ylabels_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 51-60

.. seealso::
    `.Figure.align_ylabels` and `.Figure.align_labels` for a direct method
    of doing the same thing.
    Also :doc:`/gallery/subplots_axes_and_figures/align_labels_demo`


Or we can manually align the axis labels between subplots manually using the
`~.Axis.set_label_coords` method of the y-axis object.  Note this requires
we know a good offset value which is hardcoded.

.. GENERATED FROM PYTHON SOURCE LINES 61-74

.. code-block:: Python


    fig, axs = plt.subplots(2, 2)
    fig.subplots_adjust(left=0.2, wspace=0.6)

    make_plot(axs)

    labelx = -0.3  # axes coords

    for j in range(2):
        axs[j, 1].yaxis.set_label_coords(labelx, 0.5)

    plt.show()




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_align_ylabels_002.png
   :alt: ylabels not aligned, ylabels aligned
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_align_ylabels_002.png, /gallery/text_labels_and_annotations/images/sphx_glr_align_ylabels_002_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 75-86

.. admonition:: References

   The use of the following functions, methods, classes and modules is shown
   in this example:

   - `matplotlib.figure.Figure.align_ylabels`
   - `matplotlib.axis.Axis.set_label_coords`
   - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot`
   - `matplotlib.axes.Axes.set_title`
   - `matplotlib.axes.Axes.set_ylabel`
   - `matplotlib.axes.Axes.set_ylim`


.. _sphx_glr_download_gallery_text_labels_and_annotations_align_ylabels.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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