
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "gallery/text_labels_and_annotations/tex_demo.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_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:


===============================
Render math equations using TeX
===============================

You can use TeX to render all of your Matplotlib text by setting
:rc:`text.usetex` to True.  This requires that you have TeX and the other
dependencies described in the :ref:`usetex` tutorial properly
installed on your system.  Matplotlib caches processed TeX expressions, so that
only the first occurrence of an expression triggers a TeX compilation. Later
occurrences reuse the rendered image from the cache and are thus faster.

Unicode input is supported, e.g. for the y-axis label in this example.

.. GENERATED FROM PYTHON SOURCE LINES 15-33

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    plt.rcParams['text.usetex'] = True


    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')




.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_001.png
   :alt: \TeX\ is Number $\displaystyle\sum_{n=1}^\infty\frac{-e^{i\pi}}{2^n}$!
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_001.png, /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_001_2_00x.png 2.00x
   :class: sphx-glr-single-img





.. GENERATED FROM PYTHON SOURCE LINES 34-35

A more complex example.

.. GENERATED FROM PYTHON SOURCE LINES 35-93

.. code-block:: Python


    fig, ax = plt.subplots()
    # interface tracking profiles
    N = 500
    delta = 0.6
    X = np.linspace(-1, 1, N)
    ax.plot(X, (1 - np.tanh(4 * X / delta)) / 2,    # phase field tanh profiles
            X, (1.4 + np.tanh(4 * X / delta)) / 4, "C2",  # composition profile
            X, X < 0, "k--")                        # sharp interface

    # legend
    ax.legend(("phase field", "level set", "sharp interface"),
              shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16)

    # the arrow
    ax.annotate("", xy=(-delta / 2., 0.1), xytext=(delta / 2., 0.1),
                arrowprops=dict(arrowstyle="<->", connectionstyle="arc3"))
    ax.text(0, 0.1, r"$\delta$",
            color="black", fontsize=24,
            horizontalalignment="center", verticalalignment="center",
            bbox=dict(boxstyle="round", fc="white", ec="black", pad=0.2))

    # Use tex in labels
    ax.set_xticks([-1, 0, 1])
    ax.set_xticklabels(["$-1$", r"$\pm 0$", "$+1$"], color="k", size=20)

    # Left Y-axis labels, combine math mode and text mode
    ax.set_ylabel(r"\bf{phase field} $\phi$", color="C0", fontsize=20)
    ax.set_yticks([0, 0.5, 1])
    ax.set_yticklabels([r"\bf{0}", r"\bf{.5}", r"\bf{1}"], color="k", size=20)

    # Right Y-axis labels
    ax.text(1.02, 0.5, r"\bf{level set} $\phi$",
            color="C2", fontsize=20, rotation=90,
            horizontalalignment="left", verticalalignment="center",
            clip_on=False, transform=ax.transAxes)

    # Use multiline environment inside a `text`.
    # level set equations
    eq1 = (r"\begin{eqnarray*}"
           r"|\nabla\phi| &=& 1,\\"
           r"\frac{\partial \phi}{\partial t} + U|\nabla \phi| &=& 0 "
           r"\end{eqnarray*}")
    ax.text(1, 0.9, eq1, color="C2", fontsize=18,
            horizontalalignment="right", verticalalignment="top")

    # phase field equations
    eq2 = (r"\begin{eqnarray*}"
           r"\mathcal{F} &=& \int f\left( \phi, c \right) dV, \\ "
           r"\frac{ \partial \phi } { \partial t } &=& -M_{ \phi } "
           r"\frac{ \delta \mathcal{F} } { \delta \phi }"
           r"\end{eqnarray*}")
    ax.text(0.18, 0.18, eq2, color="C0", fontsize=16)

    ax.text(-1, .30, r"gamma: $\gamma$", color="r", fontsize=20)
    ax.text(-1, .18, r"Omega: $\Omega$", color="b", fontsize=20)

    plt.show()



.. image-sg:: /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_002.png
   :alt: tex demo
   :srcset: /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_002.png, /gallery/text_labels_and_annotations/images/sphx_glr_tex_demo_002_2_00x.png 2.00x
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

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


.. _sphx_glr_download_gallery_text_labels_and_annotations_tex_demo.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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