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
<pclass="last">Click <aclass="reference internal" href="#sphx-glr-download-tutorials-text-mathtext-py"><spanclass="std std-ref">here</span></a> to download the full example code</p>
<spanid="sphx-glr-tutorials-text-mathtext-py"></span><h1>Writing mathematical expressions<aclass="headerlink" href="#writing-mathematical-expressions" title="Permalink to this headline">¶</a></h1>
<p>An introduction to writing mathematical expressions in Matplotlib.</p>
<p>You can use a subset 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 (with are designed
to blend well with Times), or a Unicode font that you provide. The mathtext
font can be selected with the customization variable <codeclass="docutils literal notranslate"><spanclass="pre">mathtext.fontset</span></code> (see
<aclass="reference internal" href="../introductory/customizing.html"><spanclass="doc">Customizing Matplotlib with style sheets and rcParams</span></a>)</p>
<pclass="last">have special meaning outside of math mode in TeX. Therefore, these
characters will behave differently depending on <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=text.usetex#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["text.usetex"]</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>
<h2>Subscripts and superscripts<aclass="headerlink" href="#subscripts-and-superscripts" title="Permalink to this headline">¶</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-3842b45874.png" style="position: relative; bottom: -6px"/> from <imgsrc="../../_images/mathmpl/math-e5cadf28b9.png" style="position: relative; bottom: -3px"/> to
<imgsrc="../../_images/mathmpl/math-a761b7733e.png" style="position: relative; bottom: -3px"/>, you could do:</p>
<h2>Fractions, binomials, and stacked numbers<aclass="headerlink" href="#fractions-binomials-and-stacked-numbers" title="Permalink to this headline">¶</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 headline">¶</a></h2>
<p>The default font is <em>italics</em> for mathematical symbols.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">This default can be changed using <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=mathtext.default#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["mathtext.default"]</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>