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
<divid="unreleased-message"> You are reading an old version of the documentation (v1.2.1). For the latest version see <ahref="/stable/">https://matplotlib.org/stable/</a></div>
<spanid="mathtext-tutorial"></span><h1>Writing mathematical expressions<aclass="headerlink" href="#writing-mathematical-expressions" title="Permalink to this headline">¶</a></h1>
<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 <ttclass="docutils literal"><spanclass="pre">usetex</span></tt> option for those who do want to call out to TeX
to generate their text (see <aclass="reference internal" href="usetex.html#usetex-tutorial"><em>Text rendering With LaTeX</em></a>).</p>
<p>Any text element can use math text. You should use raw strings
(preceed the quotes with an <ttclass="docutils literal"><spanclass="pre">'r'</span></tt>), 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 the Computer
Modern fonts (from (La)TeX), <aclass="reference external" href="http://www.aip.org/stixfonts/">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 <ttclass="docutils literal"><spanclass="pre">mathtext.fontset</span></tt> (see
of Python, if you use the STIX fonts you should also set
<ttclass="docutils literal"><spanclass="pre">ps.fonttype</span></tt> and <ttclass="docutils literal"><spanclass="pre">pdf.fonttype</span></tt> to 3 (the default), not 42.
Otherwise <aclass="reference external" href="http://thread.gmane.org/gmane.comp.python.matplotlib.general/19963/focus=19978">some characters will not be visible</a>.</p>
<pclass="last">have special meaning outside of math mode in TeX. Therefore, these
characters will behave differently depending on the rcParam
<ttclass="docutils literal"><spanclass="pre">text.usetex</span></tt> flag. See the <aclass="reference internal" href="usetex.html#usetex-tutorial"><em>usetex tutorial</em></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 <ttclass="docutils literal"><spanclass="pre">'_'</span></tt> and <ttclass="docutils literal"><spanclass="pre">'^'</span></tt> symbols:</p>
<imgsrc="../_images/mathmpl/math-e2db32b4d0.png" class="center" /><p>Some symbols automatically put their sub/superscripts under and over
the operator. For example, to write the sum of <imgsrc="../_images/mathmpl/math-ccea9f4e30.png" style="position: relative; bottom: -7px"/> from <imgsrc="../_images/mathmpl/math-f9f98764da.png" style="position: relative; bottom: -3px"/> to
<imgsrc="../_images/mathmpl/math-c899412e92.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
<ttclass="docutils literal"><spanclass="pre">\frac{}{}</span></tt>, <ttclass="docutils literal"><spanclass="pre">\binom{}{}</span></tt> and <ttclass="docutils literal"><spanclass="pre">\stackrel{}{}</span></tt> commands,
<imgsrc="../_images/mathmpl/math-07b17ba8f6.png" class="center" /><p>The solution is to precede the bracket with <ttclass="docutils literal"><spanclass="pre">\left</span></tt> and <ttclass="docutils literal"><spanclass="pre">\right</span></tt>
to inform the parser that those brackets encompass the entire object:</p>
<imgsrc="../_images/mathmpl/math-77926a09ec.png" class="center" /><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 caligraphy <ttclass="docutils literal"><spanclass="pre">A</span></tt> is squished into the <ttclass="docutils literal"><spanclass="pre">sin</span></tt>. You
can use a spacing command to add a little whitespace between them:</p>
<p>Additionally, you can use <ttclass="docutils literal"><spanclass="pre">\mathdefault{...}</span></tt> or its alias
<ttclass="docutils literal"><spanclass="pre">\mathregular{...}</span></tt> to use the font used for regular text outside of
mathtext. There are a number of limitations to this approach, most
notably that far fewer symbols will be available, but it can be useful
to make math expressions blend well with other text in the plot.</p>
<divclass="section" id="custom-fonts">
<h3>Custom fonts<aclass="headerlink" href="#custom-fonts" title="Permalink to this headline">¶</a></h3>
<p>mathtext also provides a way to use custom fonts for math. This
method is fairly tricky to use, and should be considered an
experimental feature for patient users only. By setting the rcParam
<ttclass="docutils literal"><spanclass="pre">mathtext.fontset</span></tt> to <ttclass="docutils literal"><spanclass="pre">custom</span></tt>, you can then set the following
parameters, which control which font file to use for a particular set
<p>Each parameter should be set to a fontconfig font descriptor (as
defined in the yet-to-be-written font chapter).</p>
<p>The fonts used should have a Unicode mapping in order to find any
non-Latin characters, such as Greek. If you want to use a math symbol
that is not contained in your custom fonts, you can set the rcParam
<ttclass="docutils literal"><spanclass="pre">mathtext.fallback_to_cm</span></tt> to <ttclass="docutils literal"><spanclass="pre">True</span></tt> which will cause the mathtext
system to use characters from the default Computer Modern fonts
whenever a particular character can not be found in the custom font.</p>
<p>Note that the math glyphs specified in Unicode have evolved over time,
and many fonts may not have glyphs in the correct place for mathtext.</p>
</div>
</div>
<divclass="section" id="accents">
<h2>Accents<aclass="headerlink" href="#accents" title="Permalink to this headline">¶</a></h2>
<p>An accent command may precede any symbol to add an accent above it.
There are long and short forms for some of them.</p>
<blockquote>
<div><tableborder="1" class="docutils">
<colgroup>
<colwidth="48%" />
<colwidth="52%" />
</colgroup>
<theadvalign="bottom">
<trclass="row-odd"><thclass="head">Command</th>
<thclass="head">Result</th>
</tr>
</thead>
<tbodyvalign="top">
<trclass="row-even"><td><ttclass="docutils literal"><spanclass="pre">\acute</span><spanclass="pre">a</span></tt> or <ttclass="docutils literal"><spanclass="pre">\'a</span></tt></td>
<trclass="row-odd"><td><ttclass="docutils literal"><spanclass="pre">\ddot</span><spanclass="pre">a</span></tt> or <ttclass="docutils literal"><spanclass="pre">\"a</span></tt></td>
<trclass="row-even"><td><ttclass="docutils literal"><spanclass="pre">\dot</span><spanclass="pre">a</span></tt> or <ttclass="docutils literal"><spanclass="pre">\.a</span></tt></td>
<trclass="row-odd"><td><ttclass="docutils literal"><spanclass="pre">\grave</span><spanclass="pre">a</span></tt> or <ttclass="docutils literal"><spanclass="pre">\`a</span></tt></td>
<trclass="row-even"><td><ttclass="docutils literal"><spanclass="pre">\hat</span><spanclass="pre">a</span></tt> or <ttclass="docutils literal"><spanclass="pre">\^a</span></tt></td>
<trclass="row-odd"><td><ttclass="docutils literal"><spanclass="pre">\tilde</span><spanclass="pre">a</span></tt> or <ttclass="docutils literal"><spanclass="pre">\~a</span></tt></td>