You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
<spanid="sphx-glr-tutorials-text-mathtext-py"></span><h1>Writing mathematical expressions<aclass="headerlink" href="#writing-mathematical-expressions" title="Permalink to this heading">#</a></h1>
<p>You can use a subset of TeX markup in any Matplotlib text string by placing it
inside a pair of dollar signs ($).</p>
<p>Note that you do not need to have TeX installed, since Matplotlib ships
its own TeX expression parser, layout engine, and fonts. The layout engine
is a fairly direct adaptation of the layout algorithms in Donald Knuth's
TeX, so the quality is quite good (Matplotlib also provides a <codeclass="docutils literal notranslate"><spanclass="pre">usetex</span></code>
option for those who do want to call out to TeX to generate their text; see
<aclass="reference internal" href="usetex.html"><spanclass="doc">Text rendering with LaTeX</span></a>).</p>
<p>Any text element can use math text. You should use raw strings (precede the
quotes with an <codeclass="docutils literal notranslate"><spanclass="pre">'r'</span></code>), and surround the math text with dollar signs ($), as
in TeX. Regular text and mathtext can be interleaved within the same string.
Mathtext can use DejaVu Sans (default), DejaVu Serif, the Computer Modern fonts
(from (La)TeX), <aclass="reference external" href="http://www.stixfonts.org/">STIX</a> fonts (which are designed
to blend well with Times), or a Unicode font that you provide. The mathtext
font can be selected via <codeclass="docutils literal notranslate"><aclass="reference internal" href="../introductory/customizing.html?highlight=mathtext.fontset#matplotlibrc-sample"><spanclass="std std-ref"><spanclass="pre">rcParams["mathtext.fontset"]</span></span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">'dejavusans'</span></code>) (see
<aclass="reference internal" href="../introductory/customizing.html"><spanclass="doc">Customizing Matplotlib with style sheets and rcParams</span></a>)</p>
<p>have special meaning outside of math mode in TeX. Therefore, these
characters will behave differently depending on <codeclass="docutils literal notranslate"><aclass="reference internal" href="../introductory/customizing.html?highlight=text.usetex#matplotlibrc-sample"><spanclass="std std-ref"><spanclass="pre">rcParams["text.usetex"]</span></span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>). See the
<aclass="reference internal" href="usetex.html"><spanclass="doc">usetex tutorial</span></a> for more information.</p>
</div>
<sectionid="subscripts-and-superscripts">
<h2>Subscripts and superscripts<aclass="headerlink" href="#subscripts-and-superscripts" title="Permalink to this heading">#</a></h2>
<p>To make subscripts and superscripts, use the <codeclass="docutils literal notranslate"><spanclass="pre">'_'</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">'^'</span></code> symbols:</p>
<p>Some symbols automatically put their sub/superscripts under and over the
operator. For example, to write the sum of <imgsrc="../../_images/mathmpl/math-3a2e7f36d3.png" srcset="../../_images/mathmpl/math-3a2e7f36d3.png, ../../_images/mathmpl/math-3a2e7f36d3-2x.png 2x" style="position: relative; bottom: -3px"/> from <imgsrc="../../_images/mathmpl/math-b69eb39e09.png" srcset="../../_images/mathmpl/math-b69eb39e09.png, ../../_images/mathmpl/math-b69eb39e09-2x.png 2x" style="position: relative; bottom: -2px"/> to
<imgsrc="../../_images/mathmpl/math-918327c3a6.png" srcset="../../_images/mathmpl/math-918327c3a6.png, ../../_images/mathmpl/math-918327c3a6-2x.png 2x" style="position: relative; bottom: -2px"/>, you could do:</p>
<h2>Fractions, binomials, and stacked numbers<aclass="headerlink" href="#fractions-binomials-and-stacked-numbers" title="Permalink to this heading">#</a></h2>
<p>Fractions, binomials, and stacked numbers can be created with the
<p>The solution is to precede the bracket with <codeclass="docutils literal notranslate"><spanclass="pre">\left</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">\right</span></code> to inform
the parser that those brackets encompass the entire object.:</p>
<spanid="mathtext-fonts"></span><h2>Fonts<aclass="headerlink" href="#fonts" title="Permalink to this heading">#</a></h2>
<p>The default font is <em>italics</em> for mathematical symbols.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>This default can be changed using <codeclass="docutils literal notranslate"><aclass="reference internal" href="../introductory/customizing.html?highlight=mathtext.default#matplotlibrc-sample"><spanclass="std std-ref"><spanclass="pre">rcParams["mathtext.default"]</span></span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">'it'</span></code>). This is
useful, for example, to use the same font as regular non-math text for math
text, by setting it to <codeclass="docutils literal notranslate"><spanclass="pre">regular</span></code>.</p>
</div>
<p>To change fonts, e.g., to write "sin" in a Roman font, enclose the text in a
<p>Here "s" and "t" are variable in italics font (default), "sin" is in Roman
font, and the amplitude "A" is in calligraphy font. Note in the example above
the calligraphy <codeclass="docutils literal notranslate"><spanclass="pre">A</span></code> is squished into the <codeclass="docutils literal notranslate"><spanclass="pre">sin</span></code>. You can use a spacing
command to add a little whitespace between them:</p>
<p>There are also five global "font sets" to choose from, which are
selected using the <codeclass="docutils literal notranslate"><spanclass="pre">mathtext.fontset</span></code> parameter in <aclass="reference internal" href="../introductory/customizing.html#matplotlibrc-sample"><spanclass="std std-ref">matplotlibrc</span></a>.</p>