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.0.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, <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">W3C</span></code>
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/_as_gen/matplotlib.figure.Figure.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
<spanclass="n">fig</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure" title="View documentation for matplotlib.pyplot.figure"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">figure</span></a><spanclass="p">()</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">text</span><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>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">text</span><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>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">text</span><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>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<spanclass="n">x1</span><spanclass="o">=</span><ahref="https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html#numpy.linspace" title="View documentation for numpy.linspace"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">linspace</span></a><spanclass="p">(</span><spanclass="mf">0.0</span><spanclass="p">,</span><spanclass="mf">5.0</span><spanclass="p">,</span><spanclass="mi">100</span><spanclass="p">)</span>
<spanclass="n">y1</span><spanclass="o">=</span><ahref="https://docs.scipy.org/doc/numpy/reference/generated/numpy.cos.html#numpy.cos" title="View documentation for numpy.cos"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">cos</span></a><spanclass="p">(</span><spanclass="mi">2</span><spanclass="o">*</span><ahref="https://docs.scipy.org/doc/numpy/reference/constants.html#numpy.pi" title="View documentation for numpy.pi"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">pi</span></a><spanclass="o">*</span><spanclass="n">x1</span><spanclass="p">)</span><spanclass="o">*</span><ahref="https://docs.scipy.org/doc/numpy/reference/generated/numpy.exp.html#numpy.exp" title="View documentation for numpy.exp"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">exp</span></a><spanclass="p">(</span><spanclass="o">-</span><spanclass="n">x1</span><spanclass="p">)</span>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">))</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><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
<em>labelpad</em> kwarg.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">))</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<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
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="kn">from</span><spanclass="nn">matplotlib.font_manager</span><spanclass="kn">import</span><ahref="../../api/font_manager_api.html#matplotlib.font_manager.FontProperties" title="View documentation for matplotlib.font_manager.FontProperties"><spanclass="n">FontProperties</span></a>
<spanclass="n">font</span><spanclass="o">=</span><ahref="../../api/font_manager_api.html#matplotlib.font_manager.FontProperties" title="View documentation for matplotlib.font_manager.FontProperties"><spanclass="n">FontProperties</span></a><spanclass="p">()</span>
<spanclass="n">font</span><spanclass="o">.</span><spanclass="n">set_name</span><spanclass="p">(</span><spanclass="s1">'Times New Roman'</span><spanclass="p">)</span>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">))</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">plot</span><spanclass="p">(</span><spanclass="n">x1</span><spanclass="p">,</span><ahref="https://docs.scipy.org/doc/numpy/reference/generated/numpy.cumsum.html#numpy.cumsum" title="View documentation for numpy.cumsum"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">cumsum</span></a><spanclass="p">(</span><spanclass="n">y1</span><spanclass="o">**</span><spanclass="mi">2</span><spanclass="p">))</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_xlabel</span><spanclass="p">(</span><spanclass="s1">'time [s] </span><spanclass="se">\n</span><spanclass="s1"> This was a long experiment'</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_title</span><spanclass="p">(</span><spanclass="s1">'Title with loc at '</span><spanclass="o">+</span><spanclass="n">loc</span><spanclass="p">,</span><spanclass="n">loc</span><spanclass="o">=</span><spanclass="n">loc</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<p>Vertical spacing for titles is controlled via <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html#matplotlib-rcparams"><spanclass="pre">rcParams["axes.titlepad"]</span></a></code>, which
defaults to 5 points. Setting to a different value moves the title.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">))</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<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 the best it can 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#module-matplotlib.axis" title="matplotlib.axis"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.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
<p>An Axis object has major and minor ticks. The Axis has a
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.xaxis.set_major_locator</span></code> and
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.xaxis.set_minor_locator</span></code> methods that use the data being plotted
to determine
the location of major and minor ticks. There are also
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.xaxis.set_major_formatter</span></code> and
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">matplotlib.xaxis.set_minor_formatters</span></code> methods that format the tick labels.</p>
</div>
<divclass="section" id="simple-ticks">
<h3>Simple ticks<aclass="headerlink" href="#simple-ticks" title="Permalink to this headline">¶</a></h3>
<p>It often is convenient to simply define the
tick values, and sometimes the tick labels, overriding the default
locators and formatters. This is discouraged because it breaks itneractive
navigation of the plot. It also can reset the axis limits: note that
the second plot has the ticks we asked for, including ones that are
well outside the automatic view limits.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">axs</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">),</span><spanclass="n">tight_layout</span><spanclass="o">=</span><spanclass="bp">True</span><spanclass="p">)</span>
<spanclass="n">axs</span><spanclass="p">[</span><spanclass="mi">1</span><spanclass="p">]</span><spanclass="o">.</span><spanclass="n">xaxis</span><spanclass="o">.</span><spanclass="n">set_ticks</span><spanclass="p">(</span><ahref="https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html#numpy.arange" title="View documentation for numpy.arange"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">arange</span></a><spanclass="p">(</span><spanclass="mf">0.</span><spanclass="p">,</span><spanclass="mf">8.1</span><spanclass="p">,</span><spanclass="mf">2.</span><spanclass="p">))</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<spanclass="n">ticks</span><spanclass="o">=</span><ahref="https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html#numpy.arange" title="View documentation for numpy.arange"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">arange</span></a><spanclass="p">(</span><spanclass="mf">0.</span><spanclass="p">,</span><spanclass="mf">8.1</span><spanclass="p">,</span><spanclass="mf">2.</span><spanclass="p">)</span>
<spanclass="c1"># list comprehension to get all tick labels...</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<h3>Tick Locators and Formatters<aclass="headerlink" href="#tick-locators-and-formatters" title="Permalink to this headline">¶</a></h3>
<p>Instead of making a list of all the tickalbels, we could have
used a <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> and passed it to the
<spanclass="n">ticks</span><spanclass="o">=</span><ahref="https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html#numpy.arange" title="View documentation for numpy.arange"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">arange</span></a><spanclass="p">(</span><spanclass="mf">0.</span><spanclass="p">,</span><spanclass="mf">8.1</span><spanclass="p">,</span><spanclass="mf">2.</span><spanclass="p">)</span>
<spanclass="c1"># list comprehension to get all tick labels...</span>
<spanclass="n">formatter</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.StrMethodFormatter" title="View documentation for matplotlib.ticker.StrMethodFormatter"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">StrMethodFormatter</span></a><spanclass="p">(</span><spanclass="s1">'{x:1.1f}'</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<spanclass="n">formatter</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.FormatStrFormatter" title="View documentation for matplotlib.ticker.FormatStrFormatter"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">FormatStrFormatter</span></a><spanclass="p">(</span><spanclass="s1">'</span><spanclass="si">%1.1f</span><spanclass="s1">'</span><spanclass="p">)</span>
<spanclass="n">locator</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.FixedLocator" title="View documentation for matplotlib.ticker.FixedLocator"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">FixedLocator</span></a><spanclass="p">(</span><spanclass="n">ticks</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<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
<spanclass="n">formatter</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.FormatStrFormatter" title="View documentation for matplotlib.ticker.FormatStrFormatter"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">FormatStrFormatter</span></a><spanclass="p">(</span><spanclass="s1">'</span><spanclass="si">%1.1f</span><spanclass="s1">'</span><spanclass="p">)</span>
<spanclass="n">locator</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.MaxNLocator" title="View documentation for matplotlib.ticker.MaxNLocator"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">MaxNLocator</span></a><spanclass="p">(</span><spanclass="n">nbins</span><spanclass="o">=</span><spanclass="s1">'auto'</span><spanclass="p">,</span><spanclass="n">steps</span><spanclass="o">=</span><spanclass="p">[</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="mi">4</span><spanclass="p">,</span><spanclass="mi">10</span><spanclass="p">])</span>
<spanclass="n">formatter</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.FormatStrFormatter" title="View documentation for matplotlib.ticker.FormatStrFormatter"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">FormatStrFormatter</span></a><spanclass="p">(</span><spanclass="s1">'</span><spanclass="si">%1.5f</span><spanclass="s1">'</span><spanclass="p">)</span>
<spanclass="n">locator</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.AutoLocator" title="View documentation for matplotlib.ticker.AutoLocator"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">AutoLocator</span></a><spanclass="p">()</span>
<spanclass="n">formatter</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.FormatStrFormatter" title="View documentation for matplotlib.ticker.FormatStrFormatter"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">FormatStrFormatter</span></a><spanclass="p">(</span><spanclass="s1">'</span><spanclass="si">%1.5f</span><spanclass="s1">'</span><spanclass="p">)</span>
<spanclass="n">locator</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.MaxNLocator" title="View documentation for matplotlib.ticker.MaxNLocator"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">MaxNLocator</span></a><spanclass="p">(</span><spanclass="n">nbins</span><spanclass="o">=</span><spanclass="mi">4</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">),</span><spanclass="n">tight_layout</span><spanclass="o">=</span><spanclass="bp">True</span><spanclass="p">)</span>
<spanclass="n">formatter</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.FuncFormatter" title="View documentation for matplotlib.ticker.FuncFormatter"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">FuncFormatter</span></a><spanclass="p">(</span><spanclass="n">formatoddticks</span><spanclass="p">)</span>
<spanclass="n">locator</span><spanclass="o">=</span><ahref="../../api/ticker_api.html#matplotlib.ticker.MaxNLocator" title="View documentation for matplotlib.ticker.MaxNLocator"><spanclass="n">matplotlib</span><spanclass="o">.</span><spanclass="n">ticker</span><spanclass="o">.</span><spanclass="n">MaxNLocator</span></a><spanclass="p">(</span><spanclass="n">nbins</span><spanclass="o">=</span><spanclass="mi">6</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
objects as plotting arguments. Dates and times require special
formatting, which can often benefit from manual intervention. In
order to help, dates have spectial 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>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">),</span><spanclass="n">tight_layout</span><spanclass="o">=</span><spanclass="bp">True</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<spanclass="n">locator</span><spanclass="o">=</span><ahref="../../api/dates_api.html#matplotlib.dates.AutoDateLocator" title="View documentation for matplotlib.dates.AutoDateLocator"><spanclass="n">mdates</span><spanclass="o">.</span><spanclass="n">AutoDateLocator</span></a><spanclass="p">(</span><spanclass="n">interval_multiples</span><spanclass="o">=</span><spanclass="bp">True</span><spanclass="p">)</span>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">),</span><spanclass="n">tight_layout</span><spanclass="o">=</span><spanclass="bp">True</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>
<p>However, this changes the tick labels. The easiest fix is to 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
<codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">dates.DayLocator</span></code> 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> module.</p>
<divclass="highlight-python notranslate"><divclass="highlight"><pre><span></span><spanclass="n">locator</span><spanclass="o">=</span><ahref="../../api/dates_api.html#matplotlib.dates.DayLocator" title="View documentation for matplotlib.dates.DayLocator"><spanclass="n">mdates</span><spanclass="o">.</span><spanclass="n">DayLocator</span></a><spanclass="p">(</span><spanclass="n">bymonthday</span><spanclass="o">=</span><spanclass="p">[</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="mi">15</span><spanclass="p">])</span>
<spanclass="n">formatter</span><spanclass="o">=</span><ahref="../../api/dates_api.html#matplotlib.dates.DateFormatter" title="View documentation for matplotlib.dates.DateFormatter"><spanclass="n">mdates</span><spanclass="o">.</span><spanclass="n">DateFormatter</span></a><spanclass="p">(</span><spanclass="s1">'%b </span><spanclass="si">%d</span><spanclass="s1">'</span><spanclass="p">)</span>
<spanclass="n">fig</span><spanclass="p">,</span><spanclass="n">ax</span><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="View documentation for matplotlib.pyplot.subplots"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="n">figsize</span><spanclass="o">=</span><spanclass="p">(</span><spanclass="mi">5</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">),</span><spanclass="n">tight_layout</span><spanclass="o">=</span><spanclass="bp">True</span><spanclass="p">)</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="View documentation for matplotlib.pyplot.show"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span>