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

    Click :ref:`here <sphx_glr_download_gallery_pyplots_text_layout.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_gallery_pyplots_text_layout.py:


===========
Text Layout
===========





.. image:: /gallery/pyplots/images/sphx_glr_text_layout_001.png
    :class: sphx-glr-single-img





.. code-block:: python

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches

    # build a rectangle in axes coords
    left, width = .25, .5
    bottom, height = .25, .5
    right = left + width
    top = bottom + height

    fig = plt.figure()
    ax = fig.add_axes([0,0,1,1])

    # axes coordinates are 0,0 is bottom left and 1,1 is upper right
    p = patches.Rectangle(
        (left, bottom), width, height,
        fill=False, transform=ax.transAxes, clip_on=False
        )

    ax.add_patch(p)

    ax.text(left, bottom, 'left top',
            horizontalalignment='left',
            verticalalignment='top',
            transform=ax.transAxes)

    ax.text(left, bottom, 'left bottom',
            horizontalalignment='left',
            verticalalignment='bottom',
            transform=ax.transAxes)

    ax.text(right, top, 'right bottom',
            horizontalalignment='right',
            verticalalignment='bottom',
            transform=ax.transAxes)

    ax.text(right, top, 'right top',
            horizontalalignment='right',
            verticalalignment='top',
            transform=ax.transAxes)

    ax.text(right, bottom, 'center top',
            horizontalalignment='center',
            verticalalignment='top',
            transform=ax.transAxes)

    ax.text(left, 0.5*(bottom+top), 'right center',
            horizontalalignment='right',
            verticalalignment='center',
            rotation='vertical',
            transform=ax.transAxes)

    ax.text(left, 0.5*(bottom+top), 'left center',
            horizontalalignment='left',
            verticalalignment='center',
            rotation='vertical',
            transform=ax.transAxes)

    ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
            horizontalalignment='center',
            verticalalignment='center',
            fontsize=20, color='red',
            transform=ax.transAxes)

    ax.text(right, 0.5*(bottom+top), 'centered',
            horizontalalignment='center',
            verticalalignment='center',
            rotation='vertical',
            transform=ax.transAxes)

    ax.text(left, top, 'rotated\nwith newlines',
            horizontalalignment='center',
            verticalalignment='center',
            rotation=45,
            transform=ax.transAxes)

    ax.set_axis_off()
    plt.show()


.. _sphx_glr_download_gallery_pyplots_text_layout.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: text_layout.ipynb <text_layout.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>`_
