{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Render math equations using TeX\n\nYou can use TeX to render all of your Matplotlib text by setting\n:rc:`text.usetex` to True. This requires that you have TeX and the other\ndependencies described in the `usetex` tutorial properly\ninstalled on your system. Matplotlib caches processed TeX expressions, so that\nonly the first occurrence of an expression triggers a TeX compilation. Later\noccurrences reuse the rendered image from the cache and are thus faster.\n\nUnicode input is supported, e.g. for the y-axis label in this example.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\nplt.rcParams['text.usetex'] = True\n\n\nt = np.linspace(0.0, 1.0, 100)\ns = np.cos(4 * np.pi * t) + 2\n\nfig, ax = plt.subplots(figsize=(6, 4), tight_layout=True)\nax.plot(t, s)\n\nax.set_xlabel(r'\\textbf{time (s)}')\nax.set_ylabel('\\\\textit{Velocity (\\N{DEGREE SIGN}/sec)}', fontsize=16)\nax.set_title(r'\\TeX\\ is Number $\\displaystyle\\sum_{n=1}^\\infty'\n r'\\frac{-e^{i\\pi}}{2^n}$!', fontsize=16, color='r')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A more complex example.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig, ax = plt.subplots()\n# interface tracking profiles\nN = 500\ndelta = 0.6\nX = np.linspace(-1, 1, N)\nax.plot(X, (1 - np.tanh(4 * X / delta)) / 2, # phase field tanh profiles\n X, (1.4 + np.tanh(4 * X / delta)) / 4, \"C2\", # composition profile\n X, X < 0, \"k--\") # sharp interface\n\n# legend\nax.legend((\"phase field\", \"level set\", \"sharp interface\"),\n shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=16)\n\n# the arrow\nax.annotate(\"\", xy=(-delta / 2., 0.1), xytext=(delta / 2., 0.1),\n arrowprops=dict(arrowstyle=\"<->\", connectionstyle=\"arc3\"))\nax.text(0, 0.1, r\"$\\delta$\",\n color=\"black\", fontsize=24,\n horizontalalignment=\"center\", verticalalignment=\"center\",\n bbox=dict(boxstyle=\"round\", fc=\"white\", ec=\"black\", pad=0.2))\n\n# Use tex in labels\nax.set_xticks([-1, 0, 1])\nax.set_xticklabels([\"$-1$\", r\"$\\pm 0$\", \"$+1$\"], color=\"k\", size=20)\n\n# Left Y-axis labels, combine math mode and text mode\nax.set_ylabel(r\"\\bf{phase field} $\\phi$\", color=\"C0\", fontsize=20)\nax.set_yticks([0, 0.5, 1])\nax.set_yticklabels([r\"\\bf{0}\", r\"\\bf{.5}\", r\"\\bf{1}\"], color=\"k\", size=20)\n\n# Right Y-axis labels\nax.text(1.02, 0.5, r\"\\bf{level set} $\\phi$\",\n color=\"C2\", fontsize=20, rotation=90,\n horizontalalignment=\"left\", verticalalignment=\"center\",\n clip_on=False, transform=ax.transAxes)\n\n# Use multiline environment inside a `text`.\n# level set equations\neq1 = (r\"\\begin{eqnarray*}\"\n r\"|\\nabla\\phi| &=& 1,\\\\\"\n r\"\\frac{\\partial \\phi}{\\partial t} + U|\\nabla \\phi| &=& 0 \"\n r\"\\end{eqnarray*}\")\nax.text(1, 0.9, eq1, color=\"C2\", fontsize=18,\n horizontalalignment=\"right\", verticalalignment=\"top\")\n\n# phase field equations\neq2 = (r\"\\begin{eqnarray*}\"\n r\"\\mathcal{F} &=& \\int f\\left( \\phi, c \\right) dV, \\\\ \"\n r\"\\frac{ \\partial \\phi } { \\partial t } &=& -M_{ \\phi } \"\n r\"\\frac{ \\delta \\mathcal{F} } { \\delta \\phi }\"\n r\"\\end{eqnarray*}\")\nax.text(0.18, 0.18, eq2, color=\"C0\", fontsize=16)\n\nax.text(-1, .30, r\"gamma: $\\gamma$\", color=\"r\", fontsize=20)\nax.text(-1, .18, r\"Omega: $\\Omega$\", color=\"b\", fontsize=20)\n\nplt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }