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-text-intro-py"></span><h1>Text in Matplotlib Plots<aclass="headerlink" href="#text-in-matplotlib-plots" title="Permalink to this heading">#</a></h1>
<p>Introduction to plotting and working with text in Matplotlib.</p>
<p>Matplotlib has extensive text support, including support for
mathematical expressions, truetype support for raster and
vector outputs, newline separated text with arbitrary
rotations, and Unicode support.</p>
<p>Because it embeds fonts directly in output documents, e.g., for postscript
or PDF, what you see on the screen is what you get in the hardcopy.
<aclass="reference external" href="https://www.freetype.org/">FreeType</a> support
produces very nice, antialiased fonts, that look good even at small
raster sizes. Matplotlib includes its own
<aclass="reference internal" href="../../api/font_manager_api.html#module-matplotlib.font_manager" title="matplotlib.font_manager"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">matplotlib.font_manager</span></code></a> (thanks to Paul Barrett), which
implements a cross platform, <aclass="reference external" href="https://www.w3.org/">W3C</a>
compliant font finding algorithm.</p>
<p>The user has a great deal of control over text properties (font size, font
weight, text location and color, etc.) with sensible defaults set in
the <aclass="reference internal" href="../introductory/customizing.html"><spanclass="doc">rc file</span></a>.
And significantly, for those interested in mathematical
or scientific figures, Matplotlib implements a large number of TeX
math symbols and commands, supporting <aclass="reference internal" href="mathtext.html"><spanclass="doc">mathematical expressions</span></a> anywhere in your figure.</p>
<sectionid="basic-text-commands">
<h2>Basic text commands<aclass="headerlink" href="#basic-text-commands" title="Permalink to this heading">#</a></h2>
<p>The following commands are used to create text in the implicit and explicit
interfaces (see <aclass="reference internal" href="../../users/explain/api_interfaces.html#api-interfaces"><spanclass="std std-ref">Matplotlib Application Interfaces (APIs)</span></a> for an explanation of the tradeoffs):</p>
<td><p>Add a title to the <aclass="reference internal" href="../../api/figure_api.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Figure</span></code></a>.</p></td>
</tr>
</tbody>
</table>
<p>All of these functions create and return a <aclass="reference internal" href="../../api/text_api.html#matplotlib.text.Text" title="matplotlib.text.Text"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Text</span></code></a> instance, which can be
configured with a variety of font and other properties. The example below
shows all of these commands in action, and more detail is provided in the
<ahref="../../api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text" title="matplotlib.axes.Axes.text" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">text</span></a><spanclass="p">(</span><spanclass="mi">3</span><spanclass="p">,</span><spanclass="mi">8</span><spanclass="p">,</span><spanclass="s1">'boxed italics text in data coords'</span><spanclass="p">,</span><spanclass="n">style</span><spanclass="o">=</span><spanclass="s1">'italic'</span><spanclass="p">,</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text" title="matplotlib.axes.Axes.text" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">text</span></a><spanclass="p">(</span><spanclass="mi">3</span><spanclass="p">,</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="s1">'Unicode: Institut für Festkörperphysik'</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.text.html#matplotlib.axes.Axes.text" title="matplotlib.axes.Axes.text" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">text</span></a><spanclass="p">(</span><spanclass="mf">0.95</span><spanclass="p">,</span><spanclass="mf">0.01</span><spanclass="p">,</span><spanclass="s1">'colored text in axes coords'</span><spanclass="p">,</span>
<imgsrc="../../_images/sphx_glr_text_intro_002.png" srcset="../../_images/sphx_glr_text_intro_002.png, ../../_images/sphx_glr_text_intro_002_2_0x.png 2.0x" alt="text intro" class = "sphx-glr-single-img"/><p>The x- and y-labels are automatically placed so that they clear the x- and
y-ticklabels. Compare the plot below with that above, and note the y-label
<imgsrc="../../_images/sphx_glr_text_intro_003.png" srcset="../../_images/sphx_glr_text_intro_003.png, ../../_images/sphx_glr_text_intro_003_2_0x.png 2.0x" alt="text intro" class = "sphx-glr-single-img"/><p>If you want to move the labels, you can specify the <em>labelpad</em> keyword
argument, where the value is points (1/72", the same unit used to specify
<imgsrc="../../_images/sphx_glr_text_intro_005.png" srcset="../../_images/sphx_glr_text_intro_005.png, ../../_images/sphx_glr_text_intro_005_2_0x.png 2.0x" alt="text intro" class = "sphx-glr-single-img"/><p>All the labelling in this tutorial can be changed by manipulating the
<aclass="reference internal" href="../../api/font_manager_api.html#matplotlib.font_manager.FontProperties" title="matplotlib.font_manager.FontProperties"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.font_manager.FontProperties</span></code></a> method, or by named keyword
<imgsrc="../../_images/sphx_glr_text_intro_006.png" srcset="../../_images/sphx_glr_text_intro_006.png, ../../_images/sphx_glr_text_intro_006_2_0x.png 2.0x" alt="text intro" class = "sphx-glr-single-img"/><p>Finally, we can use native TeX rendering in all text objects and have
<ahref="../../api/_as_gen/matplotlib.axes.Axes.set_xlabel.html#matplotlib.axes.Axes.set_xlabel" title="matplotlib.axes.Axes.set_xlabel" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_xlabel</span></a><spanclass="p">(</span><spanclass="s1">'Time [s] </span><spanclass="se">\n</span><spanclass="s1"> This was a long experiment'</span><spanclass="p">)</span>
<imgsrc="../../_images/sphx_glr_text_intro_008.png" srcset="../../_images/sphx_glr_text_intro_008.png, ../../_images/sphx_glr_text_intro_008_2_0x.png 2.0x" alt="Title with loc at center, Title with loc at left, Title with loc at right" class = "sphx-glr-single-img"/><p>Vertical spacing for titles is controlled via <codeclass="docutils literal notranslate"><aclass="reference internal" href="../introductory/customizing.html?highlight=axes.titlepad#matplotlibrc-sample"><spanclass="std std-ref"><spanclass="pre">rcParams["axes.titlepad"]</span></span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">6.0</span></code>).
<imgsrc="../../_images/sphx_glr_text_intro_009.png" srcset="../../_images/sphx_glr_text_intro_009.png, ../../_images/sphx_glr_text_intro_009_2_0x.png 2.0x" alt="Vertically offset title" class = "sphx-glr-single-img"/></section>
<sectionid="ticks-and-ticklabels">
<h2>Ticks and ticklabels<aclass="headerlink" href="#ticks-and-ticklabels" title="Permalink to this heading">#</a></h2>
<p>Placing ticks and ticklabels is a very tricky aspect of making a figure.
Matplotlib does its best to accomplish the task automatically, but it also
offers a very flexible framework for determining the choices for tick
locations, and how they are labelled.</p>
<sectionid="terminology">
<h3>Terminology<aclass="headerlink" href="#terminology" title="Permalink to this heading">#</a></h3>
<p><em>Axes</em> have an <aclass="reference internal" href="../../api/axis_api.html#matplotlib.axis.Axis" title="matplotlib.axis.Axis"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.axis.Axis</span></code></a> object for the <codeclass="docutils literal notranslate"><spanclass="pre">ax.xaxis</span></code> and
<codeclass="docutils literal notranslate"><spanclass="pre">ax.yaxis</span></code> that contain the information about how the labels in the axis
are laid out.</p>
<p>The axis API is explained in detail in the documentation to
<imgsrc="../../_images/sphx_glr_text_intro_010.png" srcset="../../_images/sphx_glr_text_intro_010.png, ../../_images/sphx_glr_text_intro_010_2_0x.png 2.0x" alt="text intro" class = "sphx-glr-single-img"/><p>We can of course fix this after the fact, but it does highlight a
weakness of hard-coding the ticks. This example also changes the format
format string) or <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.FormatStrFormatter" title="matplotlib.ticker.FormatStrFormatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.ticker.FormatStrFormatter</span></code></a> (old-style '%'
format string) and passed it to the <codeclass="docutils literal notranslate"><spanclass="pre">ax.xaxis</span></code>. A
<aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.StrMethodFormatter" title="matplotlib.ticker.StrMethodFormatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.ticker.StrMethodFormatter</span></code></a> can also be created by passing a
<codeclass="docutils literal notranslate"><spanclass="pre">str</span></code> without having to explicitly create the formatter.</p>
<imgsrc="../../_images/sphx_glr_text_intro_012.png" srcset="../../_images/sphx_glr_text_intro_012.png, ../../_images/sphx_glr_text_intro_012_2_0x.png 2.0x" alt="text intro" class = "sphx-glr-single-img"/><p>And of course we could have used a non-default locator to set the
tick locations. Note we still pass in the tick values, but the
x-limit fix used above is <em>not</em> needed.</p>
<imgsrc="../../_images/sphx_glr_text_intro_013.png" srcset="../../_images/sphx_glr_text_intro_013.png, ../../_images/sphx_glr_text_intro_013_2_0x.png 2.0x" alt="text intro" class = "sphx-glr-single-img"/><p>The default formatter is the <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.MaxNLocator" title="matplotlib.ticker.MaxNLocator"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.ticker.MaxNLocator</span></code></a> called as