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
<li><aclass="reference internal" href="#types-of-inputs-to-plotting-functions">Types of inputs to plotting functions</a></li>
<li><aclass="reference internal" href="#the-object-oriented-interface-and-the-pyplot-interface">The object-oriented interface and the pyplot interface</a></li>
<pclass="last">Click <aclass="reference internal" href="#sphx-glr-download-tutorials-introductory-usage-py"><spanclass="std std-ref">here</span></a> to download the full example code</p>
<spanid="sphx-glr-tutorials-introductory-usage-py"></span><h1>Usage Guide<aclass="headerlink" href="#usage-guide" title="Permalink to this headline">¶</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="Permalink to this headline">¶</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 (i.e., windows, Jupyter
widgets, etc.), each of which can contain one or more <aclass="reference internal" href="../../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axes</span></code></a> (i.e., an
area where points can be specified in terms of x-y coordinates (or theta-r
in a polar plot, or x-y-z in a 3D plot, etc.). The most simple 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:</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="p">,</span><spanclass="n">ax</span><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>
<p>In fact, you can do the same in Matplotlib: for each <aclass="reference internal" href="../../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axes</span></code></a> graphing
method, there is a corresponding function in the <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.html#module-matplotlib.pyplot" title="matplotlib.pyplot"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">matplotlib.pyplot</span></code></a>
module that performs that plot on the "current" axes, creating that axes (and
its parent figure) if they don't exist yet. So the previous example can be
<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="Permalink to this headline">¶</a></h3>
<p>The <strong>whole</strong> figure. The figure keeps
track of all the child <aclass="reference internal" href="../../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a>, a smattering of
'special' artists (titles, figure legends, etc), and the <strong>canvas</strong>.
(Don't worry too much about the canvas, it is crucial as it is the
object that actually does the drawing to get you your plot, but as the
user it is more-or-less invisible to you). A figure can contain any
number of <aclass="reference internal" href="../../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a>, but will typically have
at least one.</p>
<p>The easiest way to create a new figure is with pyplot:</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><spanclass="n">plt</span><spanclass="o">.</span><spanclass="n">figure</span><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><spanclass="n">ax</span><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><spanclass="n">axs</span><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>
</pre></div>
</div>
<p>It's convenient to create the axes together with the figure, but you can
also add axes later on, allowing for more complex axes layouts.</p>
</div>
<divclass="section" id="axes">
<h3><aclass="reference internal" href="../../api/axes_api.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="Permalink to this headline">¶</a></h3>
<p>This is what you think of as 'a plot', it is the region of the image
with the data space. A given figure
can contain many Axes, but a given <aclass="reference internal" href="../../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a>
object can only be in one <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>. The
Axes contains 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>) which take care of the data limits (the
data limits can also be controlled via the <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set_xlim.html#matplotlib.axes.Axes.set_xlim" title="matplotlib.axes.Axes.set_xlim"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">axes.Axes.set_xlim()</span></code></a> and
<aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set_ylim.html#matplotlib.axes.Axes.set_ylim" title="matplotlib.axes.Axes.set_ylim"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">axes.Axes.set_ylim()</span></code></a> methods). Each <aclass="reference internal" href="../../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a> 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/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Axes</span></code></a> class and its member functions are the primary entry
point to working with the OO interface.</p>
</div>
<divclass="section" id="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="Permalink to this headline">¶</a></h3>
<p>These are the number-line-like objects. They take
care of setting the graph limits and generating the 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>
</div>
<divclass="section" id="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="Permalink to this headline">¶</a></h3>
<p>Basically everything you can see on the figure is an artist (even the
<spanid="input-types"></span><h2>Types of inputs to plotting functions<aclass="headerlink" href="#types-of-inputs-to-plotting-functions" title="Permalink to this headline">¶</a></h2>
<p>All of plotting functions expect <aclass="reference external" href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array" title="(in NumPy v1.17)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.array</span></code></a> or <aclass="reference external" href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.ma.masked_array.html#numpy.ma.masked_array" title="(in NumPy v1.17)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.ma.masked_array</span></code></a> as
input. Classes that are 'array-like' such as <aclass="reference external" href="https://pandas.pydata.org/pandas-docs/stable/index.html#module-pandas" title="(in pandas v1.0.3)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">pandas</span></code></a> data objects
and <aclass="reference external" href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html#numpy.matrix" title="(in NumPy v1.17)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.matrix</span></code></a> may or may not work as intended. It is best to
convert these to <aclass="reference external" href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.array.html#numpy.array" title="(in NumPy v1.17)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">numpy.array</span></code></a> objects prior to plotting.</p>
<p>For example, to convert a <aclass="reference external" href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html#pandas.DataFrame" title="(in pandas v1.0.3)"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">pandas.DataFrame</span></code></a></p>
<spanid="coding-styles"></span><h2>The object-oriented interface and the pyplot interface<aclass="headerlink" href="#the-object-oriented-interface-and-the-pyplot-interface" title="Permalink to this headline">¶</a></h2>
<p>As noted above, there are essentially two ways to use Matplotlib:</p>
<ulclass="simple">
<li>Explicitly create figures and axes, and call methods on them (the
"object-oriented (OO) style").</li>
<li>Rely on pyplot to automatically create and manage the figures and axes, and
<spanclass="c1"># Note that even in the OO-style, we use `.pyplot.figure` to create the figure.</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><spanclass="n">ax</span><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 and an 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://docs.scipy.org/doc/numpy/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://docs.scipy.org/doc/numpy/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://docs.scipy.org/doc/numpy/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://docs.scipy.org/doc/numpy/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://docs.scipy.org/doc/numpy/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://docs.scipy.org/doc/numpy/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>
<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://docs.scipy.org/doc/numpy/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://docs.scipy.org/doc/numpy/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>
<divclass="sphx-glr-script-out highlight-none notranslate"><divclass="highlight"><pre><span></span><matplotlib.legend.Legend object at 0x7fdbbc4e0160>
</pre></div>
</div>
<p>Actually there is a third approach, for the case where you are embedding
Matplotlib in a GUI application, which completely drops pyplot, even for
figure creation. We won't discuss it here; see the corresponding section in
the gallery for more info (<aclass="reference internal" href="../../gallery/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
approaches (which are equally powerful), and you should feel free to use
either (however, it is preferable pick one of them and stick to it, instead
of mixing them). In general, we suggest to restrict pyplot to interactive
plotting (e.g., in a Jupyter notebook), and to prefer the OO-style for
non-interactive plotting (in functions and scripts that are intended to be
reused as part of a larger project).</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<p>In older examples, you may find examples that instead used the so-called
<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 star-import
imports everything both from pyplot and from <aclass="reference external" href="https://docs.scipy.org/doc/numpy/reference/index.html#module-numpy" title="(in NumPy v1.17)"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">numpy</span></code></a>, so that one
<p>A more detailed description is given below.</p>
<p>If multiple of these are configurations are present, the last one from the
list takes precedence; e.g. calling <aclass="reference internal" href="../../api/matplotlib_configuration_api.html#matplotlib.use" title="matplotlib.use"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">matplotlib.use()</span></code></a> will override
the setting in your <codeclass="docutils literal notranslate"><spanclass="pre">matplotlibrc</span></code>.</p>
<p>If no backend is explicitly set, Matplotlib automatically detects a usable
backend based on what is available on your system and on whether a GUI event
loop is already running. On Linux, if the environment variable
<spanclass="target" id="index-1"></span><aclass="reference internal" href="../../faq/environment_variables_faq.html#envvar-DISPLAY"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">DISPLAY</span></code></a> is unset, the "event loop" is identified as "headless",
which causes a fallback to a noninteractive backend (agg).</p>
<p>Here is a detailed description of the configuration methods:</p>
<olclass="arabic">
<li><pclass="first">Setting <codeclass="docutils literal notranslate"><aclass="reference external" href="../../tutorials/introductory/customizing.html?highlight=backend#a-sample-matplotlibrc-file"><spanclass="pre">rcParams["backend"]</span></a></code> (default: 'agg') in your <codeclass="docutils literal notranslate"><spanclass="pre">matplotlibrc</span></code> file:</p>
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><spanclass="n">backend</span><spanclass="p">:</span><spanclass="n">qt5agg</span><spanclass="c1"># use pyqt5 with antigrain (agg) rendering</span>
</pre></div>
</div>
<p>See also <aclass="reference internal" href="customizing.html"><spanclass="doc">Customizing Matplotlib with style sheets and rcParams</span></a>.</p>
<p>Setting this environment variable will override the <codeclass="docutils literal notranslate"><spanclass="pre">backend</span></code> parameter
in <em>any</em><codeclass="docutils literal notranslate"><spanclass="pre">matplotlibrc</span></code>, even if there is a <codeclass="docutils literal notranslate"><spanclass="pre">matplotlibrc</span></code> in your
current working directory. Therefore, setting <spanclass="target" id="index-3"></span><aclass="reference internal" href="../../faq/environment_variables_faq.html#envvar-MPLBACKEND"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">MPLBACKEND</span></code></a>
globally, e.g. in your <codeclass="docutils literal notranslate"><spanclass="pre">.bashrc</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">.profile</span></code>, is discouraged as it
might lead to counter-intuitive behavior.</p>
</li>
<li><pclass="first">If your script depends on a specific backend you can use the function
<p>This should be done before any figure is created; otherwise Matplotlib may
fail to switch the backend and raise an ImportError.</p>
<p>Using <aclass="reference internal" href="../../api/matplotlib_configuration_api.html#matplotlib.use" title="matplotlib.use"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">use</span></code></a> will require changes in your code if users want to
use a different backend. Therefore, you should avoid explicitly calling
<h3>The builtin backends<aclass="headerlink" href="#the-builtin-backends" title="Permalink to this headline">¶</a></h3>
<p>By default, Matplotlib should automatically select a default backend which
allows both interactive work and plotting from scripts, with output to the
screen and/or to a file, so at least initially you will not need to worry
about the backend. The most common exception is if your Python distribution
comes without <aclass="reference external" href="https://docs.python.org/3/library/tkinter.html#module-tkinter" title="(in Python v3.8)"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">tkinter</span></code></a> and you have no other GUI toolkit installed;
this happens on certain Linux distributions, where you need to install a
Linux package named <codeclass="docutils literal notranslate"><spanclass="pre">python-tk</span></code> (or similar).</p>
<p>If, however, you want to write graphical user interfaces, or a web
application server (<aclass="reference internal" href="../../faq/howto_faq.html#howto-webapp"><spanclass="std std-ref">How to use Matplotlib in a web application server</span></a>), or need a better
understanding of what is going on, read on. To make things a little
more customizable for graphical user interfaces, matplotlib separates
the concept of the renderer (the thing that actually does the drawing)
from the canvas (the place where the drawing goes). The canonical
renderer for user interfaces is <codeclass="docutils literal notranslate"><spanclass="pre">Agg</span></code> which uses the <aclass="reference external" href="http://antigrain.com/">Anti-Grain
Geometry</a> C++ library to make a raster (pixel) image of the figure; it
is used by the <codeclass="docutils literal notranslate"><spanclass="pre">Qt5Agg</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">Qt4Agg</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">GTK3Agg</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">wxAgg</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">TkAgg</span></code>, and
<codeclass="docutils literal notranslate"><spanclass="pre">macosx</span></code> backends. An alternative renderer is based on the Cairo library,
used by <codeclass="docutils literal notranslate"><spanclass="pre">Qt5Cairo</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">Qt4Cairo</span></code>, etc.</p>
<p>For the rendering engines, one can also distinguish between <aclass="reference external" href="https://en.wikipedia.org/wiki/Vector_graphics">vector</a> or <aclass="reference external" href="https://en.wikipedia.org/wiki/Raster_graphics">raster</a> renderers. Vector
graphics languages issue drawing commands like "draw a line from this
point to this point" and hence are scale free, and raster backends
generate a pixel representation of the line whose accuracy depends on a
DPI setting.</p>
<p>Here is a summary of the matplotlib renderers (there is an eponymous
backend for each; these are <em>non-interactive backends</em>, capable of
writing to a file):</p>
<tableborder="1" class="docutils align-default">
<colgroup>
<colwidth="11%" />
<colwidth="13%" />
<colwidth="76%" />
</colgroup>
<theadvalign="bottom">
<trclass="row-odd"><thclass="head">Renderer</th>
<thclass="head">Filetypes</th>
<thclass="head">Description</th>
</tr>
</thead>
<tbodyvalign="top">
<trclass="row-even"><td>AGG</td>
<td>png</td>
<td><aclass="reference external" href="https://en.wikipedia.org/wiki/Raster_graphics">raster</a> graphics -- high quality images using the
<td><aclass="reference external" href="https://en.wikipedia.org/wiki/Raster_graphics">raster</a> or <aclass="reference external" href="https://en.wikipedia.org/wiki/Vector_graphics">vector</a> graphics -- using the <aclass="reference external" href="https://www.cairographics.org">Cairo</a> library</td>
</tr>
</tbody>
</table>
<p>To save plots using the non-interactive backends, use the
<p>And here are the user interfaces and renderer combinations supported;
these are <em>interactive backends</em>, capable of displaying to the screen
and of using appropriate renderers from the table above to write to
a file:</p>
<tableborder="1" class="docutils align-default">
<colgroup>
<colwidth="12%" />
<colwidth="88%" />
</colgroup>
<theadvalign="bottom">
<trclass="row-odd"><thclass="head">Backend</th>
<thclass="head">Description</th>
</tr>
</thead>
<tbodyvalign="top">
<trclass="row-even"><td>Qt5Agg</td>
<td>Agg rendering in a <aclass="reference internal" href="../../glossary/index.html#term-Qt5"><spanclass="xref std std-term">Qt5</span></a> canvas (requires <aclass="reference external" href="https://riverbankcomputing.com/software/pyqt/intro">PyQt5</a>). This
backend can be activated in IPython with <codeclass="docutils literal notranslate"><spanclass="pre">%matplotlib</span><spanclass="pre">qt5</span></code>.</td>
</tr>
<trclass="row-odd"><td>ipympl</td>
<td>Agg rendering embedded in a Jupyter widget. (requires ipympl).
This backend can be enabled in a Jupyter notebook with
<td>Agg rendering to a <aclass="reference internal" href="../../glossary/index.html#term-GTK"><spanclass="xref std std-term">GTK</span></a> 3.x canvas (requires <aclass="reference external" href="https://wiki.gnome.org/action/show/Projects/PyGObject">PyGObject</a>,
and <aclass="reference external" href="https://www.cairographics.org/pycairo/">pycairo</a> or <aclass="reference external" href="https://pythonhosted.org/cairocffi/">cairocffi</a>). This backend can be activated in
IPython with <codeclass="docutils literal notranslate"><spanclass="pre">%matplotlib</span><spanclass="pre">gtk3</span></code>.</td>
</tr>
<trclass="row-odd"><td>macosx</td>
<td>Agg rendering into a Cocoa canvas in OSX. This backend can be
activated in IPython with <codeclass="docutils literal notranslate"><spanclass="pre">%matplotlib</span><spanclass="pre">osx</span></code>.</td>
</tr>
<trclass="row-even"><td>TkAgg</td>
<td>Agg rendering to a <aclass="reference internal" href="../../glossary/index.html#term-Tk"><spanclass="xref std std-term">Tk</span></a> canvas (requires <aclass="reference external" href="https://docs.python.org/3/library/tk.html">TkInter</a>). This
backend can be activated in IPython with <codeclass="docutils literal notranslate"><spanclass="pre">%matplotlib</span><spanclass="pre">tk</span></code>.</td>
</tr>
<trclass="row-odd"><td>nbAgg</td>
<td>Embed an interactive figure in a Jupyter classic notebook. This
<td>On <codeclass="docutils literal notranslate"><spanclass="pre">show()</span></code> will start a tornado server with an interactive
figure.</td>
</tr>
<trclass="row-odd"><td>GTK3Cairo</td>
<td>Cairo rendering to a <aclass="reference internal" href="../../glossary/index.html#term-GTK"><spanclass="xref std std-term">GTK</span></a> 3.x canvas (requires <aclass="reference external" href="https://wiki.gnome.org/action/show/Projects/PyGObject">PyGObject</a>,
and <aclass="reference external" href="https://www.cairographics.org/pycairo/">pycairo</a> or <aclass="reference external" href="https://pythonhosted.org/cairocffi/">cairocffi</a>).</td>
</tr>
<trclass="row-even"><td>Qt4Agg</td>
<td>Agg rendering to a <aclass="reference internal" href="../../glossary/index.html#term-Qt4"><spanclass="xref std std-term">Qt4</span></a> canvas (requires <aclass="reference external" href="https://riverbankcomputing.com/software/pyqt/intro">PyQt4</a> or
<codeclass="docutils literal notranslate"><spanclass="pre">pyside</span></code>). This backend can be activated in IPython with
<td>Agg rendering to a <aclass="reference internal" href="../../glossary/index.html#term-wxWidgets"><spanclass="xref std std-term">wxWidgets</span></a> canvas (requires <aclass="reference external" href="https://www.wxpython.org/">wxPython</a> 4).
This backend can be activated in IPython with <codeclass="docutils literal notranslate"><spanclass="pre">%matplotlib</span><spanclass="pre">wx</span></code>.</td>
</tr>
</tbody>
</table>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">The names of builtin backends case-insensitive; e.g., 'Qt5Agg' and
'qt5agg' are equivalent.</p>
</div>
<divclass="section" id="ipympl">
<h4>ipympl<aclass="headerlink" href="#ipympl" title="Permalink to this headline">¶</a></h4>
<p>The Jupyter widget ecosystem is moving too fast to support directly in
<spanid="qt-api-usage"></span><h4>How do I select PyQt4 or PySide?<aclass="headerlink" href="#how-do-i-select-pyqt4-or-pyside" title="Permalink to this headline">¶</a></h4>
<p>The <spanclass="target" id="index-4"></span><aclass="reference internal" href="../../faq/environment_variables_faq.html#envvar-QT_API"><codeclass="xref std std-envvar docutils literal notranslate"><spanclass="pre">QT_API</span></code></a> environment variable can be set to either <codeclass="docutils literal notranslate"><spanclass="pre">pyqt</span></code> or
<codeclass="docutils literal notranslate"><spanclass="pre">pyside</span></code> to use <codeclass="docutils literal notranslate"><spanclass="pre">PyQt4</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">PySide</span></code>, respectively.</p>
<p>Since the default value for the bindings to be used is <codeclass="docutils literal notranslate"><spanclass="pre">PyQt4</span></code>, Matplotlib
first tries to import it, if the import fails, it tries to import <codeclass="docutils literal notranslate"><spanclass="pre">PySide</span></code>.</p>
<spanid="interactive-mode"></span><h2>What is interactive mode?<aclass="headerlink" href="#what-is-interactive-mode" title="Permalink to this headline">¶</a></h2>
<p>Use of an interactive backend (see <aclass="reference internal" href="#what-is-a-backend"><spanclass="std std-ref">What is a backend?</span></a>)
permits--but does not by itself require or ensure--plotting
to the screen. Whether and when plotting to the screen occurs,
and whether a script or shell session continues after a plot
is drawn on the screen, depends on the functions and methods
that are called, and on a state variable that determines whether
matplotlib is in "interactive mode". The default Boolean value is set
by the <codeclass="file docutils literal notranslate"><spanclass="pre">matplotlibrc</span></code> file, and may be customized like any other
configuration parameter (see <aclass="reference internal" href="customizing.html"><spanclass="doc">Customizing Matplotlib with style sheets and rcParams</span></a>). It
may also be set via <aclass="reference internal" href="../../api/matplotlib_configuration_api.html#matplotlib.interactive" title="matplotlib.interactive"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">matplotlib.interactive()</span></code></a>, and its
value may be queried via <aclass="reference internal" href="../../api/matplotlib_configuration_api.html#matplotlib.is_interactive" title="matplotlib.is_interactive"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">matplotlib.is_interactive()</span></code></a>. Turning
interactive mode on and off in the middle of a stream of plotting
commands, whether in a script or in a shell, is rarely needed
and potentially confusing, so in the following we will assume all
plotting is done with interactive mode either on or off.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">Major changes related to interactivity, and in particular the
role and behavior of <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">show()</span></code></a>, were made in the
transition to matplotlib version 1.0, and bugs were fixed in
1.0.1. Here we describe the version 1.0.1 behavior for the
primary interactive backends, with the partial exception of
<em>macosx</em>.</p>
</div>
<p>Interactive mode may also be turned on via <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.ion.html#matplotlib.pyplot.ion" title="matplotlib.pyplot.ion"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">matplotlib.pyplot.ion()</span></code></a>,
and turned off via <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.ioff.html#matplotlib.pyplot.ioff" title="matplotlib.pyplot.ioff"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">matplotlib.pyplot.ioff()</span></code></a>.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">Interactive mode works with suitable backends in ipython and in
the ordinary python shell, but it does <em>not</em> work in the IDLE IDE.
If the default backend does not support interactivity, an interactive
backend can be explicitly activated using any of the methods discussed
in <aclass="reference internal" href="#id2">What is a backend?</a>.</p>
</div>
<divclass="section" id="interactive-example">
<h3>Interactive example<aclass="headerlink" href="#interactive-example" title="Permalink to this headline">¶</a></h3>
<p>From an ordinary python prompt, or after invoking ipython with no options,
<p>On most interactive backends, the figure window will also be updated if you
change it via the object-oriented interface. E.g. get a reference to the
<aclass="reference internal" href="../../api/axes_api.html#matplotlib.axes.Axes" title="matplotlib.axes.Axes"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axes</span></code></a> instance, and call a method of that instance:</p>
<p>If you are using certain backends (like <codeclass="docutils literal notranslate"><spanclass="pre">macosx</span></code>), or an older version
of matplotlib, you may not see the new line added to the plot immediately.
In this case, you need to explicitly call <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.draw.html#matplotlib.pyplot.draw" title="matplotlib.pyplot.draw"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">draw()</span></code></a>
object method calls in addition to pyplot functions, then
call <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.draw.html#matplotlib.pyplot.draw" title="matplotlib.pyplot.draw"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">draw()</span></code></a> whenever you want to
refresh the plot.</p>
<p>Use non-interactive mode in scripts in which you want to
generate one or more figures and display them before ending
or generating a new set of figures. In that case, use
<aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.show.html#matplotlib.pyplot.show" title="matplotlib.pyplot.show"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">show()</span></code></a> to display the figure(s) and
to block execution until you have manually destroyed them.</p>
</div>
</div>
<divclass="section" id="performance">
<spanid="id3"></span><h2>Performance<aclass="headerlink" href="#performance" title="Permalink to this headline">¶</a></h2>
<p>Whether exploring data in interactive mode or programmatically
saving lots of plots, rendering performance can be a painful
bottleneck in your pipeline. Matplotlib provides a couple
ways to greatly reduce rendering time at the cost of a slight
change (to a settable tolerance) in your plot's appearance.
The methods available to reduce rendering time depend on the
<h3>Splitting lines into smaller chunks<aclass="headerlink" href="#splitting-lines-into-smaller-chunks" title="Permalink to this headline">¶</a></h3>
<p>If you are using the Agg backend (see <aclass="reference internal" href="#what-is-a-backend"><spanclass="std std-ref">What is a backend?</span></a>),
then you can make use of the <codeclass="docutils literal notranslate"><spanclass="pre">agg.path.chunksize</span></code> rc parameter.
This allows you to specify a chunk size, and any lines with
greater than that many vertices will be split into multiple
lines, each of which has no more than <codeclass="docutils literal notranslate"><spanclass="pre">agg.path.chunksize</span></code>
many vertices. (Unless <codeclass="docutils literal notranslate"><spanclass="pre">agg.path.chunksize</span></code> is zero, in
which case there is no chunking.) For some kind of data,
chunking the line up into reasonable sizes can greatly
decrease rendering time.</p>
<p>The following script will first display the data without any
chunk size restriction, and then display the same data with
a chunk size of 10,000. The difference can best be seen when
the figures are large, try maximizing the GUI and then