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

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

.. _sphx_glr_gallery_text_labels_and_annotations_tex_demo.py:


=================================
Rendering math equation using TeX
=================================

You can use TeX to render all of your matplotlib text if the rc
parameter ``text.usetex`` is set.  This works currently on the agg and ps
backends, and requires that you have tex and the other dependencies
described in the :doc:`/tutorials/text/usetex` tutorial
properly installed on your system.  The first time you run a script
you will see a lot of output from tex and associated tools.  The next
time, the run may be silent, as a lot of the information is cached.

Notice how the label for the y axis is provided using unicode!





.. image:: /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_001.png
    :class: sphx-glr-single-img





.. code-block:: python

    import numpy as np
    import matplotlib
    matplotlib.rcParams['text.usetex'] = True
    import matplotlib.pyplot as plt


    t = np.linspace(0.0, 1.0, 100)
    s = np.cos(4 * np.pi * t) + 2

    fig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)
    ax.plot(t, s)

    ax.set_xlabel(r'\textbf{time (s)}')
    ax.set_ylabel('\\textit{Velocity (\N{DEGREE SIGN}/sec)}', fontsize=16)
    ax.set_title(r'\TeX\ is Number $\displaystyle\sum_{n=1}^\infty'
                 r'\frac{-e^{i\pi}}{2^n}$!', fontsize=16, color='r')
    plt.show()


.. _sphx_glr_download_gallery_text_labels_and_annotations_tex_demo.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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