

.. _sphx_glr_gallery_recipes_placing_text_boxes.py:


Placing text boxes
==================

When decorating axes with text boxes, two useful tricks are to place
the text in axes coordinates (see :ref:`sphx_glr_tutorials_03_advanced_transforms_tutorial.py`), so the
text doesn't move around with changes in x or y limits.  You can also
use the ``bbox`` property of text to surround the text with a
:class:`~matplotlib.patches.Patch` instance -- the ``bbox`` keyword
argument takes a dictionary with keys that are Patch properties.




.. image:: /gallery/recipes/images/sphx_glr_placing_text_boxes_001.png
    :align: center





.. code-block:: python


    import numpy as np
    import matplotlib.pyplot as plt

    np.random.seed(1234)

    fig, ax = plt.subplots()
    x = 30*np.random.randn(10000)
    mu = x.mean()
    median = np.median(x)
    sigma = x.std()
    textstr = '$\mu=%.2f$\n$\mathrm{median}=%.2f$\n$\sigma=%.2f$' % (mu, median, sigma)

    ax.hist(x, 50)
    # these are matplotlib.patch.Patch properties
    props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)

    # place a text box in upper left in axes coords
    ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14,
            verticalalignment='top', bbox=props)

**Total running time of the script:** ( 0 minutes  0.051 seconds)



.. container:: sphx-glr-footer


  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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

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

    `Generated by Sphinx-Gallery <https://sphinx-gallery.readthedocs.io>`_
