
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/pyplots/auto_subplots_adjust.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_gallery_pyplots_auto_subplots_adjust.py>`
        to download the full example code

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

.. _sphx_glr_gallery_pyplots_auto_subplots_adjust.py:


====================
Auto Subplots Adjust
====================

Automatically adjust subplot parameters. This example shows a way to determine
a subplot parameter from the extent of the ticklabels using a callback on the
:doc:`draw_event</users/event_handling>`.

Note that a similar result would be achieved using `~.Figure.tight_layout`
or `~.Figure.set_constrained_layout`; this example shows how one could
customize the subplot parameter adjustment.

.. GENERATED FROM PYTHON SOURCE LINES 14-42

.. code-block:: default


    import matplotlib.pyplot as plt
    import matplotlib.transforms as mtransforms

    fig, ax = plt.subplots()
    ax.plot(range(10))
    ax.set_yticks((2, 5, 7))
    labels = ax.set_yticklabels(('really, really, really', 'long', 'labels'))

    def on_draw(event):
        bboxes = []
        for label in labels:
            bbox = label.get_window_extent()
            # the figure transform goes from relative coords->pixels and we
            # want the inverse of that
            bboxi = bbox.transformed(fig.transFigure.inverted())
            bboxes.append(bboxi)
        # the bbox that bounds all the bboxes, again in relative figure coords
        bbox = mtransforms.Bbox.union(bboxes)
        if fig.subplotpars.left < bbox.width:
            # we need to move it over
            fig.subplots_adjust(left=1.1*bbox.width)  # pad a little
            fig.canvas.draw()

    fig.canvas.mpl_connect('draw_event', on_draw)

    plt.show()




.. image:: /gallery/pyplots/images/sphx_glr_auto_subplots_adjust_001.png
    :alt: auto subplots adjust
    :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 43-50

------------

References
""""""""""

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

.. GENERATED FROM PYTHON SOURCE LINES 51-61

.. code-block:: default


    import matplotlib
    matplotlib.artist.Artist.get_window_extent
    matplotlib.transforms.Bbox
    matplotlib.transforms.Bbox.transformed
    matplotlib.transforms.Bbox.union
    matplotlib.transforms.Transform.inverted
    matplotlib.figure.Figure.subplots_adjust
    matplotlib.figure.SubplotParams
    matplotlib.backend_bases.FigureCanvasBase.mpl_connect




.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none


    <function FigureCanvasBase.mpl_connect at 0x7f73bed5fb80>




.. _sphx_glr_download_gallery_pyplots_auto_subplots_adjust.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

    Keywords: matplotlib code example, codex, python plot, pyplot
    `Gallery generated by Sphinx-Gallery
    <https://sphinx-gallery.readthedocs.io>`_
