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>
<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-ccea9f4e30.png" style="position: relative; bottom: -6px"/> 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
<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 the <codeclass="docutils literal notranslate"><spanclass="pre">mathtext.default</span></code> rcParam. 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 three 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>
<p><codeclass="docutils literal notranslate"><spanclass="pre">cm</span></code>: <strong>Computer Modern (TeX)</strong></p>
<p>Additionally, you can use <codeclass="docutils literal notranslate"><spanclass="pre">\mathdefault{...}</span></code> or its alias
<codeclass="docutils literal notranslate"><spanclass="pre">\mathregular{...}</span></code> 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 <codeclass="docutils literal notranslate"><spanclass="pre">mathtext.fontset</span></code> to <codeclass="docutils literal notranslate"><spanclass="pre">custom</span></code>,
you can then set the following parameters, which control which font file to use
<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
<codeclass="docutils literal notranslate"><spanclass="pre">mathtext.fallback_to_cm</span></code> to <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> 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><codeclass="docutils literal notranslate"><spanclass="pre">\acute</span><spanclass="pre">a</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">\'a</span></code></td>