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
<liclass="toctree-l2"><aclass="reference internal" href="figure/writing_a_backend_pyplot_interface.html">Writing a backend -- the pyplot interface</a></li>
</ul>
</details></li>
</ul>
<ulclass="nav bd-sidenav">
<liclass="toctree-l1 has-children"><aclass="reference internal" href="axes/index.html">Axes and subplots</a><details><summary><spanclass="toctree-toggle" role="presentation"><iclass="fa-solid fa-chevron-down"></i></span></summary><ul>
<liclass="toctree-l2"><aclass="reference internal" href="axes/axes_intro.html">Introduction to Axes (or Subplots)</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="axes/arranging_axes.html">Arranging multiple Axes in a Figure</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="text/pgf.html">Text rendering with XeLaTeX/LuaLaTeX via the <codeclass="docutils literal notranslate"><spanclass="pre">pgf</span></code> backend</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="text/usetex.html">Text rendering with LaTeX</a></li>
</ul>
</details></li>
</ul>
<ulclass="nav bd-sidenav">
<liclass="toctree-l1 has-children"><aclass="reference internal" href="animations/index.html">Animations using Matplotlib</a><details><summary><spanclass="toctree-toggle" role="presentation"><iclass="fa-solid fa-chevron-down"></i></span></summary><ul>
<liclass="toctree-l2"><aclass="reference internal" href="animations/animations.html">Animations using Matplotlib</a></li>
<liclass="toctree-l2"><aclass="reference internal" href="animations/blitting.html">Faster rendering by using blitting</a></li>
<spanid="quick-start"></span><spanid="sphx-glr-users-explain-quick-start-py"></span><h1>Quick start guide<aclass="headerlink" href="#quick-start-guide" title="Link to this heading">#</a></h1>
<p>This tutorial covers some basic usage patterns and best practices to
<h2>A simple example<aclass="headerlink" href="#a-simple-example" title="Link to this heading">#</a></h2>
<p>Matplotlib graphs your data on <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>s (e.g., windows, Jupyter
widgets, etc.), each of which can contain one or more <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axes</span></code></a>, an
area where points can be specified in terms of x-y coordinates (or theta-r
in a polar plot, x-y-z in a 3D plot, etc.). The simplest way of
creating a Figure with an Axes is using <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="matplotlib.pyplot.subplots"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">pyplot.subplots</span></code></a>. We can then use
<aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot" title="matplotlib.axes.Axes.plot"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axes.plot</span></code></a> to draw some data on the Axes, and <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">show</span></code></a> to display
the figure:</p>
<divclass="highlight-Python notranslate"><divclass="highlight"><pre><span></span><ahref="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure" class="sphx-glr-backref-module-matplotlib-figure sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">fig</span></a><spanclass="p">,</span><ahref="../../api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">ax</span></a><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="matplotlib.pyplot.subplots" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">()</span><spanclass="c1"># Create a figure containing a single Axes.</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot" title="matplotlib.axes.Axes.plot" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">plot</span></a><spanclass="p">([</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">,</span><spanclass="mi">4</span><spanclass="p">],</span><spanclass="p">[</span><spanclass="mi">1</span><spanclass="p">,</span><spanclass="mi">4</span><spanclass="p">,</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="mi">3</span><spanclass="p">])</span><spanclass="c1"># Plot some data on the Axes.</span>
<ahref="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">show</span></a><spanclass="p">()</span><spanclass="c1"># Show the figure.</span>
</pre></div>
</div>
<imgsrc="../../_images/sphx_glr_quick_start_001.png" srcset="../../_images/sphx_glr_quick_start_001.png, ../../_images/sphx_glr_quick_start_001_2_00x.png 2.00x" alt="quick start" class = "sphx-glr-single-img"/><p>Depending on the environment you are working in, <codeclass="docutils literal notranslate"><spanclass="pre">plt.show()</span></code> can be left
out. This is for example the case with Jupyter notebooks, which
automatically show all figures created in a code cell.</p>
</section>
<sectionid="parts-of-a-figure">
<spanid="figure-parts"></span><h2>Parts of a Figure<aclass="headerlink" href="#parts-of-a-figure" title="Link to this heading">#</a></h2>
<p>Here are the components of a Matplotlib Figure.</p>
<h3><aclass="reference internal" href="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Figure</span></code></a><aclass="headerlink" href="#figure" title="Link to this heading">#</a></h3>
<p>The <strong>whole</strong> figure. The Figure keeps
track of all the child <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a>, a group of
'special' Artists (titles, figure legends, colorbars, etc.), and
even nested subfigures.</p>
<p>Typically, you'll create a new Figure through one of the following
functions:</p>
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><ahref="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure" class="sphx-glr-backref-module-matplotlib-figure sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">fig</span></a><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.figure.html#matplotlib.pyplot.figure" title="matplotlib.pyplot.figure" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">figure</span></a><spanclass="p">()</span><spanclass="c1"># an empty figure with no Axes</span>
<ahref="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure" class="sphx-glr-backref-module-matplotlib-figure sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">fig</span></a><spanclass="p">,</span><ahref="../../api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">ax</span></a><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="matplotlib.pyplot.subplots" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">()</span><spanclass="c1"># a figure with a single Axes</span>
<ahref="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure" class="sphx-glr-backref-module-matplotlib-figure sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">fig</span></a><spanclass="p">,</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">axs</span></a><spanclass="o">=</span><ahref="../../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="matplotlib.pyplot.subplots" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">subplots</span></a><spanclass="p">(</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="mi">2</span><spanclass="p">)</span><spanclass="c1"># a figure with a 2x2 grid of Axes</span>
<spanclass="c1"># a figure with one Axes on the left, and two on the right:</span>
that additionally create Axes objects inside the Figure, but you can also
manually add Axes later on.</p>
<p>For more on Figures, including panning and zooming, see <aclass="reference internal" href="figure/figure_intro.html#figure-intro"><spanclass="std std-ref">Introduction to Figures</span></a>.</p>
</section>
<sectionid="axes">
<h3><aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a><aclass="headerlink" href="#axes" title="Link to this heading">#</a></h3>
<p>An Axes is an Artist attached to a Figure that contains a region for
plotting data, and usually includes two (or three in the case of 3D)
<aclass="reference internal" href="../../api/axis_api.html#matplotlib.axis.Axis" title="matplotlib.axis.Axis"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axis</span></code></a> objects (be aware of the difference
between <strong>Axes</strong> and <strong>Axis</strong>) that provide ticks and tick labels to
provide scales for the data in the Axes. Each <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a> also
has a title
(set via <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set_title.html#matplotlib.axes.Axes.set_title" title="matplotlib.axes.Axes.set_title"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">set_title()</span></code></a>), an x-label (set via
<aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set_xlabel.html#matplotlib.axes.Axes.set_xlabel" title="matplotlib.axes.Axes.set_xlabel"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">set_xlabel()</span></code></a>), and a y-label set via
<p>The <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axes</span></code></a> methods are the primary interface for configuring
most parts of your plot (adding data, controlling axis scales and
limits, adding labels etc.).</p>
</section>
<sectionid="axis">
<h3><aclass="reference internal" href="../../api/axis_api.html#matplotlib.axis.Axis" title="matplotlib.axis.Axis"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axis</span></code></a><aclass="headerlink" href="#axis" title="Link to this heading">#</a></h3>
<p>These objects set the scale and limits and generate ticks (the marks
on the Axis) and ticklabels (strings labeling the ticks). The location
of the ticks is determined by a <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.Locator" title="matplotlib.ticker.Locator"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Locator</span></code></a> object and the
ticklabel strings are formatted by a <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.Formatter" title="matplotlib.ticker.Formatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Formatter</span></code></a>. The
combination of the correct <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.Locator" title="matplotlib.ticker.Locator"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Locator</span></code></a> and <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.Formatter" title="matplotlib.ticker.Formatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Formatter</span></code></a> gives very fine
control over the tick locations and labels.</p>
</section>
<sectionid="artist">
<h3><aclass="reference internal" href="../../api/artist_api.html#matplotlib.artist.Artist" title="matplotlib.artist.Artist"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Artist</span></code></a><aclass="headerlink" href="#artist" title="Link to this heading">#</a></h3>
<p>Basically, everything visible on the Figure is an Artist (even
<spanid="input-types"></span><h2>Types of inputs to plotting functions<aclass="headerlink" href="#types-of-inputs-to-plotting-functions" title="Link to this heading">#</a></h2>
input, or objects that can be passed to <aclass="reference external" href="https://numpy.org/doc/stable/reference/generated/numpy.asarray.html#numpy.asarray" title="(in NumPy v2.4)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.asarray</span></code></a>.
Classes that are similar to arrays ('array-like') such as <aclass="reference external" href="https://pandas.pydata.org/pandas-docs/stable/index.html#module-pandas" title="(in pandas v3.0.2)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">pandas</span></code></a>
data objects and <aclass="reference external" href="https://numpy.org/doc/stable/reference/generated/numpy.matrix.html#numpy.matrix" title="(in NumPy v2.4)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.matrix</span></code></a> may not work as intended. Common convention
is to convert these to <aclass="reference external" href="https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array" title="(in NumPy v2.4)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.array</span></code></a> objects prior to plotting.
For example, to convert a <aclass="reference external" href="https://numpy.org/doc/stable/reference/generated/numpy.matrix.html#numpy.matrix" title="(in NumPy v2.4)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.matrix</span></code></a></p>
<p>Most methods will also parse a string-indexable object like a <em>dict</em>, a
<aclass="reference external" href="https://numpy.org/doc/stable/user/basics.rec.html#structured-arrays">structured numpy array</a>, or a <aclass="reference external" href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame" title="(in pandas v3.0.2)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">pandas.DataFrame</span></code></a>. Matplotlib allows you
to provide the <codeclass="docutils literal notranslate"><spanclass="pre">data</span></code> keyword argument and generate plots passing the
strings corresponding to the <em>x</em> and <em>y</em> variables.</p>
<divclass="highlight-Python notranslate"><divclass="highlight"><pre><span></span><ahref="https://numpy.org/doc/stable/reference/random/generated/numpy.random.seed.html#numpy.random.seed" title="numpy.random.seed" class="sphx-glr-backref-module-numpy-random sphx-glr-backref-type-py-function"><spanclass="n">np</span><spanclass="o">.</span><spanclass="n">random</span><spanclass="o">.</span><spanclass="n">seed</span></a><spanclass="p">(</span><spanclass="mi">19680801</span><spanclass="p">)</span><spanclass="c1"># seed the random number generator.</span>
<h3>The explicit and the implicit interfaces<aclass="headerlink" href="#the-explicit-and-the-implicit-interfaces" title="Link to this heading">#</a></h3>
<p>As noted above, there are essentially two ways to use Matplotlib:</p>
<ulclass="simple">
<li><p>Explicitly create Figures and Axes, and call methods on them (the
"object-oriented (OO) style").</p></li>
<li><p>Rely on pyplot to implicitly create and manage the Figures and Axes, and
use pyplot functions for plotting.</p></li>
</ul>
<p>See <aclass="reference internal" href="figure/api_interfaces.html#api-interfaces"><spanclass="std std-ref">Matplotlib Application Interfaces (APIs)</span></a> for an explanation of the tradeoffs between the
<ahref="../../api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot" title="matplotlib.axes.Axes.plot" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">plot</span></a><spanclass="p">(</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="p">,</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="p">,</span><spanclass="n">label</span><spanclass="o">=</span><spanclass="s1">'linear'</span><spanclass="p">)</span><spanclass="c1"># Plot some data on the Axes.</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot" title="matplotlib.axes.Axes.plot" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">plot</span></a><spanclass="p">(</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="p">,</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="o">**</span><spanclass="mi">2</span><spanclass="p">,</span><spanclass="n">label</span><spanclass="o">=</span><spanclass="s1">'quadratic'</span><spanclass="p">)</span><spanclass="c1"># Plot more data on the Axes...</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot" title="matplotlib.axes.Axes.plot" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">plot</span></a><spanclass="p">(</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="p">,</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="o">**</span><spanclass="mi">3</span><spanclass="p">,</span><spanclass="n">label</span><spanclass="o">=</span><spanclass="s1">'cubic'</span><spanclass="p">)</span><spanclass="c1"># ... and some more.</span>
<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">'x label'</span><spanclass="p">)</span><spanclass="c1"># Add an x-label to the Axes.</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.set_ylabel.html#matplotlib.axes.Axes.set_ylabel" title="matplotlib.axes.Axes.set_ylabel" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_ylabel</span></a><spanclass="p">(</span><spanclass="s1">'y label'</span><spanclass="p">)</span><spanclass="c1"># Add a y-label to the Axes.</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.set_title.html#matplotlib.axes.Axes.set_title" title="matplotlib.axes.Axes.set_title" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">set_title</span></a><spanclass="p">(</span><spanclass="s2">"Simple Plot"</span><spanclass="p">)</span><spanclass="c1"># Add a title to the Axes.</span>
<ahref="../../api/_as_gen/matplotlib.axes.Axes.legend.html#matplotlib.axes.Axes.legend" title="matplotlib.axes.Axes.legend" class="sphx-glr-backref-module-matplotlib-axes sphx-glr-backref-type-py-method"><spanclass="n">ax</span><spanclass="o">.</span><spanclass="n">legend</span></a><spanclass="p">()</span><spanclass="c1"># Add a legend.</span>
</pre></div>
</div>
<imgsrc="../../_images/sphx_glr_quick_start_003.png" srcset="../../_images/sphx_glr_quick_start_003.png, ../../_images/sphx_glr_quick_start_003_2_00x.png 2.00x" alt="Simple Plot" class = "sphx-glr-single-img"/><p>or the pyplot-style:</p>
<ahref="../../api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot" title="matplotlib.pyplot.plot" class="sphx-glr-backref-module-matplotlib-pyplot sphx-glr-backref-type-py-function"><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">plot</span></a><spanclass="p">(</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="p">,</span><ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">x</span></a><spanclass="p">,</span><spanclass="n">label</span><spanclass="o">=</span><spanclass="s1">'linear'</span><spanclass="p">)</span><spanclass="c1"># Plot some data on the (implicit) Axes.</span>
<imgsrc="../../_images/sphx_glr_quick_start_004.png" srcset="../../_images/sphx_glr_quick_start_004.png, ../../_images/sphx_glr_quick_start_004_2_00x.png 2.00x" alt="Simple Plot" class = "sphx-glr-single-img"/><p>(In addition, there is a third approach, for the case when embedding
Matplotlib in a GUI application, which completely drops pyplot, even for
figure creation. See the corresponding section in the gallery for more info:
<aclass="reference internal" href="../../gallery/user_interfaces/index.html#user-interfaces"><spanclass="std std-ref">Embedding Matplotlib in graphical user interfaces</span></a>.)</p>
<p>Matplotlib's documentation and examples use both the OO and the pyplot
styles. In general, we suggest using the OO style, particularly for
complicated plots, and functions and scripts that are intended to be reused
as part of a larger project. However, the pyplot style can be very convenient
for quick interactive work.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>You may find older examples that use the <codeclass="docutils literal notranslate"><spanclass="pre">pylab</span></code> interface,
via <codeclass="docutils literal notranslate"><spanclass="pre">from</span><spanclass="pre">pylab</span><spanclass="pre">import</span><spanclass="pre">*</span></code>. This approach is strongly deprecated.</p>
</div>
</section>
<sectionid="making-a-helper-functions">
<h3>Making a helper functions<aclass="headerlink" href="#making-a-helper-functions" title="Link to this heading">#</a></h3>
<p>If you need to make the same plots over and over again with different data
sets, or want to easily wrap Matplotlib methods, use the recommended
<imgsrc="../../_images/sphx_glr_quick_start_005.png" srcset="../../_images/sphx_glr_quick_start_005.png, ../../_images/sphx_glr_quick_start_005_2_00x.png 2.00x" alt="quick start" class = "sphx-glr-single-img"/><p>Note that if you want to install these as a python package, or any other
customizations you could use one of the many templates on the web;
Matplotlib has one at <aclass="reference external" href="https://github.com/matplotlib/matplotlib-extension-cookiecutter">mpl-cookiecutter</a></p>
</section>
</section>
<sectionid="styling-artists">
<h2>Styling Artists<aclass="headerlink" href="#styling-artists" title="Link to this heading">#</a></h2>
<p>Most plotting methods have styling options for the Artists, accessible either
when a plotting method is called, or from a "setter" on the Artist. In the
plot below we manually set the <em>color</em>, <em>linewidth</em>, and <em>linestyle</em> of the
Artists created by <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot" title="matplotlib.axes.Axes.plot"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">plot</span></code></a>, and we set the linestyle of the second line
after the fact with <aclass="reference internal" href="../../api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D.set_linestyle" title="matplotlib.lines.Line2D.set_linestyle"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">set_linestyle</span></code></a>.</p>
<imgsrc="../../_images/sphx_glr_quick_start_006.png" srcset="../../_images/sphx_glr_quick_start_006.png, ../../_images/sphx_glr_quick_start_006_2_00x.png 2.00x" alt="quick start" class = "sphx-glr-single-img"/><sectionid="colors">
<h3>Colors<aclass="headerlink" href="#colors" title="Link to this heading">#</a></h3>
<p>Matplotlib has a very flexible array of colors that are accepted for most
Artists; see <aclass="reference internal" href="colors/colors.html#colors-def"><spanclass="std std-ref">allowable color definitions</span></a> for a
list of specifications. Some Artists will take multiple colors. i.e. for
a <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.scatter.html#matplotlib.axes.Axes.scatter" title="matplotlib.axes.Axes.scatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">scatter</span></code></a> plot, the edge of the markers can be different colors
<h3>Linewidths, linestyles, and markersizes<aclass="headerlink" href="#linewidths-linestyles-and-markersizes" title="Link to this heading">#</a></h3>
<p>Line widths are typically in typographic points (1 pt = 1/72 inch) and
available for Artists that have stroked lines. Similarly, stroked lines
can have a linestyle. See the <aclass="reference internal" href="../../gallery/lines_bars_and_markers/linestyles.html"><spanclass="doc">linestyles example</span></a>.</p>
<p>Marker size depends on the method being used. <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.plot.html#matplotlib.axes.Axes.plot" title="matplotlib.axes.Axes.plot"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">plot</span></code></a> specifies
markersize in points, and is generally the "diameter" or width of the
marker. <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.scatter.html#matplotlib.axes.Axes.scatter" title="matplotlib.axes.Axes.scatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">scatter</span></code></a> specifies markersize as approximately
proportional to the visual area of the marker. There is an array of
markerstyles available as string codes (see <aclass="reference internal" href="../../api/markers_api.html#module-matplotlib.markers" title="matplotlib.markers"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">markers</span></code></a>), or
users can define their own <aclass="reference internal" href="../../api/_as_gen/matplotlib.markers.MarkerStyle.html#matplotlib.markers.MarkerStyle" title="matplotlib.markers.MarkerStyle"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">MarkerStyle</span></code></a> (see
<imgsrc="../../_images/sphx_glr_quick_start_008.png" srcset="../../_images/sphx_glr_quick_start_008.png, ../../_images/sphx_glr_quick_start_008_2_00x.png 2.00x" alt="quick start" class = "sphx-glr-single-img"/></section>
</section>
<sectionid="labelling-plots">
<h2>Labelling plots<aclass="headerlink" href="#labelling-plots" title="Link to this heading">#</a></h2>
<sectionid="axes-labels-and-text">
<h3>Axes labels and text<aclass="headerlink" href="#axes-labels-and-text" title="Link to this heading">#</a></h3>
<p><aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set_xlabel.html#matplotlib.axes.Axes.set_xlabel" title="matplotlib.axes.Axes.set_xlabel"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">set_xlabel</span></code></a>, <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set_ylabel.html#matplotlib.axes.Axes.set_ylabel" title="matplotlib.axes.Axes.set_ylabel"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">set_ylabel</span></code></a>, and <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set_title.html#matplotlib.axes.Axes.set_title" title="matplotlib.axes.Axes.set_title"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">set_title</span></code></a> are used to
add text in the indicated locations (see <aclass="reference internal" href="text/text_intro.html#text-intro"><spanclass="std std-ref">Text in Matplotlib</span></a>
for more discussion). Text can also be directly added to plots using
<p>where the <codeclass="docutils literal notranslate"><spanclass="pre">r</span></code> preceding the title string signifies that the string is a
<em>raw</em> string and not to treat backslashes as python escapes.
Matplotlib has a built-in TeX expression parser and
layout engine, and ships its own math fonts – for details see
<aclass="reference internal" href="text/mathtext.html#mathtext"><spanclass="std std-ref">Writing mathematical expressions</span></a>. You can also use LaTeX directly to format
your text and incorporate the output directly into your display figures or
saved postscript – see <aclass="reference internal" href="text/usetex.html#usetex"><spanclass="std std-ref">Text rendering with LaTeX</span></a>.</p>
</section>
<sectionid="annotations">
<h3>Annotations<aclass="headerlink" href="#annotations" title="Link to this heading">#</a></h3>
<p>We can also annotate points on a plot, often by connecting an arrow pointing
to <em>xy</em>, to a piece of text at <em>xytext</em>:</p>
<imgsrc="../../_images/sphx_glr_quick_start_010.png" srcset="../../_images/sphx_glr_quick_start_010.png, ../../_images/sphx_glr_quick_start_010_2_00x.png 2.00x" alt="quick start" class = "sphx-glr-single-img"/><p>In this basic example, both <em>xy</em> and <em>xytext</em> are in data coordinates.
There are a variety of other coordinate systems one can choose -- see
<aclass="reference internal" href="text/annotations.html#annotations-tutorial"><spanclass="std std-ref">Basic annotation</span></a> and <aclass="reference internal" href="text/annotations.html#plotting-guide-annotation"><spanclass="std std-ref">Advanced annotation</span></a> for
<h3>Legends<aclass="headerlink" href="#legends" title="Link to this heading">#</a></h3>
<p>Often we want to identify lines or markers with a <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.legend.html#matplotlib.axes.Axes.legend" title="matplotlib.axes.Axes.legend"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axes.legend</span></code></a>:</p>
<imgsrc="../../_images/sphx_glr_quick_start_011.png" srcset="../../_images/sphx_glr_quick_start_011.png, ../../_images/sphx_glr_quick_start_011_2_00x.png 2.00x" alt="quick start" class = "sphx-glr-single-img"/><p>Legends in Matplotlib are quite flexible in layout, placement, and what
Artists they can represent. They are discussed in detail in
<h2>Axis scales and ticks<aclass="headerlink" href="#axis-scales-and-ticks" title="Link to this heading">#</a></h2>
<p>Each Axes has two (or three) <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">Axis</span></code></a> objects representing the x- and
y-axis. These control the <em>scale</em> of the Axis, the tick <em>locators</em> and the
tick <em>formatters</em>. Additional Axes can be attached to display further Axis
objects.</p>
<sectionid="scales">
<h3>Scales<aclass="headerlink" href="#scales" title="Link to this heading">#</a></h3>
<p>In addition to the linear scale, Matplotlib supplies non-linear scales,
such as a log-scale. Since log-scales are used so much there are also
direct methods like <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.loglog.html#matplotlib.axes.Axes.loglog" title="matplotlib.axes.Axes.loglog"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">loglog</span></code></a>, <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.semilogx.html#matplotlib.axes.Axes.semilogx" title="matplotlib.axes.Axes.semilogx"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">semilogx</span></code></a>, and
<aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.semilogy.html#matplotlib.axes.Axes.semilogy" title="matplotlib.axes.Axes.semilogy"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">semilogy</span></code></a>. There are a number of scales (see
<aclass="reference internal" href="../../gallery/scales/scales.html"><spanclass="doc">Scales overview</span></a> for other examples). Here we set the scale
<imgsrc="../../_images/sphx_glr_quick_start_012.png" srcset="../../_images/sphx_glr_quick_start_012.png, ../../_images/sphx_glr_quick_start_012_2_00x.png 2.00x" alt="quick start" class = "sphx-glr-single-img"/><p>The scale sets the mapping from data values to spacing along the Axis. This
happens in both directions, and gets combined into a <em>transform</em>, which
is the way that Matplotlib maps from data coordinates to Axes, Figure, or
screen coordinates. See <aclass="reference internal" href="artists/transforms_tutorial.html#transforms-tutorial"><spanclass="std std-ref">Transformations Tutorial</span></a>.</p>
</section>
<sectionid="tick-locators-and-formatters">
<h3>Tick locators and formatters<aclass="headerlink" href="#tick-locators-and-formatters" title="Link to this heading">#</a></h3>
<p>Each Axis has a tick <em>locator</em> and <em>formatter</em> that choose where along the
Axis objects to put tick marks. A simple interface to this is
<ahref="https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray" title="numpy.ndarray" class="sphx-glr-backref-module-numpy sphx-glr-backref-type-py-class sphx-glr-backref-instance"><spanclass="n">axs</span></a><spanclass="p">[</span><spanclass="mi">1</span><spanclass="p">]</span><spanclass="o">.</span><spanclass="n">set_yticks</span><spanclass="p">([</span><spanclass="o">-</span><spanclass="mf">1.5</span><spanclass="p">,</span><spanclass="mi">0</span><spanclass="p">,</span><spanclass="mf">1.5</span><spanclass="p">])</span><spanclass="c1"># note that we don't need to specify labels</span>
<imgsrc="../../_images/sphx_glr_quick_start_013.png" srcset="../../_images/sphx_glr_quick_start_013.png, ../../_images/sphx_glr_quick_start_013_2_00x.png 2.00x" alt="Automatic ticks, Manual ticks" class = "sphx-glr-single-img"/><p>Different scales can have different locators and formatters; for instance
the log-scale above uses <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.LogLocator" title="matplotlib.ticker.LogLocator"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">LogLocator</span></code></a> and <aclass="reference internal" href="../../api/ticker_api.html#matplotlib.ticker.LogFormatter" title="matplotlib.ticker.LogFormatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">LogFormatter</span></code></a>. See
<aclass="reference internal" href="../../gallery/ticks/tick-locators.html"><spanclass="doc">Tick locators</span></a> and
<aclass="reference internal" href="../../gallery/ticks/tick-formatters.html"><spanclass="doc">Tick formatters</span></a> for other formatters and
locators and information for writing your own.</p>
</section>
<sectionid="plotting-dates-and-strings">
<h3>Plotting dates and strings<aclass="headerlink" href="#plotting-dates-and-strings" title="Link to this heading">#</a></h3>
<p>Matplotlib can handle plotting arrays of dates and arrays of strings, as
well as floating point numbers. These get special locators and formatters