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 (v3.4.2). For the latest version see <ahref="https://matplotlib.org/stable/tutorials/text/text_intro.html">https://matplotlib.org/stable/tutorials/text/text_intro.html</a></div>
<pclass="last">Click <aclass="reference internal" href="#sphx-glr-download-tutorials-text-text-intro-py"><spanclass="std std-ref">here</span></a> to download the full example code</p>
<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 headline">¶</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>
<divclass="section" id="basic-text-commands">
<h2>Basic text commands<aclass="headerlink" href="#basic-text-commands" title="Permalink to this headline">¶</a></h2>
<p>The following commands are used to create text in the pyplot
<td>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>.</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>
<p>Or, the labels accept all the <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> keyword arguments, including
<em>position</em>, via which we can manually specify the label positions. Here we
put the xlabel to the far left of the axis. Note, that the y-coordinate of
this position has no effect - to adjust the y-position we need to use the
<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 kwargs to
<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>
<imgalt="Title with loc at center, Title with loc at left, Title with loc at right" class="sphx-glr-single-img" src="../../_images/sphx_glr_text_intro_008.png" />
<p>Vertical spacing for titles is controlled via <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=axes.titlepad#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["axes.titlepad"]</span></a></code> (default: <codeclass="docutils literal notranslate"><spanclass="pre">6.0</span></code>), which
defaults to 5 points. Setting to a different value moves the title.</p>
<h2>Ticks and ticklabels<aclass="headerlink" href="#ticks-and-ticklabels" title="Permalink to this headline">¶</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>
<divclass="section" id="terminology">
<h3>Terminology<aclass="headerlink" href="#terminology" title="Permalink to this headline">¶</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
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>
<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
<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>, passing a function will
objects as plotting arguments. Dates and times require special
formatting, which can often benefit from manual intervention. In
order to help, dates have special Locators and Formatters,
defined in the <aclass="reference internal" href="../../api/dates_api.html#module-matplotlib.dates" title="matplotlib.dates"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.dates</span></code></a> module.</p>
<p>A simple example is as follows. Note how we have to rotate the
tick labels so that they don't over-run each other.</p>
<p>We can pass a format to <aclass="reference internal" href="../../api/dates_api.html#matplotlib.dates.DateFormatter" title="matplotlib.dates.DateFormatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.dates.DateFormatter</span></code></a>. Also note that the
29th and the next month are very close together. We can fix this by using
the <aclass="reference internal" href="../../api/dates_api.html#matplotlib.dates.DayLocator" title="matplotlib.dates.DayLocator"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">dates.DayLocator</span></code></a> class, which allows us to specify a list of days of
the month to use. Similar formatters are listed in the <aclass="reference internal" href="../../api/dates_api.html#module-matplotlib.dates" title="matplotlib.dates"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.dates</span></code></a>