You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
<spanid="sphx-glr-tutorials-lifecycle-py"></span><h1>The Lifecycle of a Plot<aclass="headerlink" href="#the-lifecycle-of-a-plot" title="Link to this heading">#</a></h1>
<p>This tutorial aims to show the beginning, middle, and end of a single
visualization using Matplotlib. We'll begin with some raw data and
end by saving a figure of a customized visualization. Along the way we try
to highlight some neat features and best-practices using Matplotlib.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>This tutorial is based on
<aclass="reference external" href="https://pbpython.com/effective-matplotlib.html">this excellent blog post</a>
by Chris Moffitt. It was transformed into this tutorial by Chris Holdgraf.</p>
<h2>A note on the explicit vs. implicit interfaces<aclass="headerlink" href="#a-note-on-the-explicit-vs-implicit-interfaces" title="Link to this heading">#</a></h2>
<p>Matplotlib has two interfaces. For an explanation of the trade-offs between the
explicit and implicit interfaces see <aclass="reference internal" href="../users/explain/figure/api_interfaces.html#api-interfaces"><spanclass="std std-ref">Matplotlib Application Interfaces (APIs)</span></a>.</p>
<p>In the explicit object-oriented (OO) interface we directly utilize instances of
<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.Axes</span></code></a> to build up the visualization in an instance of
<aclass="reference internal" href="../api/figure_api.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">figure.Figure</span></code></a>. In the implicit interface, inspired by and modeled on
MATLAB, we use a global state-based interface which is encapsulated in the
<aclass="reference internal" href="../api/pyplot_summary.html#module-matplotlib.pyplot" title="matplotlib.pyplot"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">pyplot</span></code></a> module to plot to the "current Axes". See the <aclass="reference internal" href="pyplot.html#pyplot-tutorial"><spanclass="std std-ref">pyplot
tutorials</span></a> for a more in-depth look at the
pyplot interface.</p>
<p>Most of the terms are straightforward but the main thing to remember
is that:</p>
<ulclass="simple">
<li><p>The <aclass="reference internal" href="../api/figure_api.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Figure</span></code></a> is the final image, and may 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>.</p></li>
<li><dlclass="simple">
<dt>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> represents an individual plot (not to be confused with</dt><dd><p><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>, which refers to the x-, y-, or z-axis of a plot).</p>
</dd>
</dl>
</li>
</ul>
<p>We call methods that do the plotting directly from the Axes, which gives
us much more flexibility and power in customizing our plot.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>In general, use the explicit interface over the implicit pyplot interface
for plotting.</p>
</div>
</section>
<sectionid="our-data">
<h2>Our data<aclass="headerlink" href="#our-data" title="Link to this heading">#</a></h2>
<p>We'll use the data from the post from which this tutorial was derived.
It contains sales information for a number of companies.</p>
<h2>Getting started<aclass="headerlink" href="#getting-started" title="Link to this heading">#</a></h2>
<p>This data is naturally visualized as a barplot, with one bar per
group. To do this with the object-oriented approach, we first generate
an instance of <aclass="reference internal" href="../api/figure_api.html#matplotlib.figure.Figure" title="matplotlib.figure.Figure"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">figure.Figure</span></code></a> and
<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.Axes</span></code></a>. The Figure is like a canvas, and the Axes
is a part of that canvas on which we will make a particular visualization.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>Figures can have multiple axes on them. For information on how to do this,
see the <aclass="reference internal" href="../users/explain/axes/tight_layout_guide.html#tight-layout-guide"><spanclass="std std-ref">Tight Layout tutorial</span></a>.</p>
<imgsrc="../_images/sphx_glr_lifecycle_001.png" srcset="../_images/sphx_glr_lifecycle_001.png, ../_images/sphx_glr_lifecycle_001_2_00x.png 2.00x" alt="lifecycle" class = "sphx-glr-single-img"/><p>Now that we have an Axes instance, we can plot on top of it.</p>
<imgsrc="../_images/sphx_glr_lifecycle_003.png" srcset="../_images/sphx_glr_lifecycle_003.png, ../_images/sphx_glr_lifecycle_003_2_00x.png 2.00x" alt="lifecycle" class = "sphx-glr-single-img"/><p>The style controls many things, such as color, linewidths, backgrounds,
etc.</p>
</section>
<sectionid="customizing-the-plot">
<h2>Customizing the plot<aclass="headerlink" href="#customizing-the-plot" title="Link to this heading">#</a></h2>
<p>Now we've got a plot with the general look that we want, so let's fine-tune
it so that it's ready for print. First let's rotate the labels on the x-axis
so that they show up more clearly. We can gain access to these labels
with the <aclass="reference internal" href="../api/_as_gen/matplotlib.axes.Axes.get_xticklabels.html#matplotlib.axes.Axes.get_xticklabels" title="matplotlib.axes.Axes.get_xticklabels"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">axes.Axes.get_xticklabels()</span></code></a> method:</p>
<imgsrc="../_images/sphx_glr_lifecycle_004.png" srcset="../_images/sphx_glr_lifecycle_004.png, ../_images/sphx_glr_lifecycle_004_2_00x.png 2.00x" alt="lifecycle" class = "sphx-glr-single-img"/><p>If we'd like to set the property of many items at once, it's useful to use
the <aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.setp.html#matplotlib.pyplot.setp" title="matplotlib.pyplot.setp"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">pyplot.setp()</span></code></a> function. This will take a list (or many lists) of
Matplotlib objects, and attempt to set some style element of each one.</p>
<imgsrc="../_images/sphx_glr_lifecycle_005.png" srcset="../_images/sphx_glr_lifecycle_005.png, ../_images/sphx_glr_lifecycle_005_2_00x.png 2.00x" alt="lifecycle" class = "sphx-glr-single-img"/><p>It looks like this cut off some of the labels on the bottom. We can
tell Matplotlib to automatically make room for elements in the figures
that we create. To do this we set the <codeclass="docutils literal notranslate"><spanclass="pre">autolayout</span></code> value of our
rcParams. For more information on controlling the style, layout, and
other features of plots with rcParams, see
<aclass="reference internal" href="../users/explain/customizing.html#customizing"><spanclass="std std-ref">Customizing Matplotlib with style sheets and rcParams</span></a>.</p>
<imgsrc="../_images/sphx_glr_lifecycle_006.png" srcset="../_images/sphx_glr_lifecycle_006.png, ../_images/sphx_glr_lifecycle_006_2_00x.png 2.00x" alt="lifecycle" class = "sphx-glr-single-img"/><p>Next, we add labels to the plot. To do this with the OO interface,
we can use the <aclass="reference internal" href="../api/_as_gen/matplotlib.artist.Artist.set.html#matplotlib.artist.Artist.set" title="matplotlib.artist.Artist.set"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">Artist.set()</span></code></a> method to set properties of this
<imgsrc="../_images/sphx_glr_lifecycle_007.png" srcset="../_images/sphx_glr_lifecycle_007.png, ../_images/sphx_glr_lifecycle_007_2_00x.png 2.00x" alt="Company Revenue" class = "sphx-glr-single-img"/><p>We can also adjust the size of this plot using the <aclass="reference internal" href="../api/_as_gen/matplotlib.pyplot.subplots.html#matplotlib.pyplot.subplots" title="matplotlib.pyplot.subplots"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">pyplot.subplots()</span></code></a>
function. We can do this with the <em>figsize</em> keyword argument.</p>
<divclass="admonition note">
<pclass="admonition-title">Note</p>
<p>While indexing in NumPy follows the form (row, column), the <em>figsize</em>
keyword argument follows the form (width, height). This follows
conventions in visualization, which unfortunately are different from those
<imgsrc="../_images/sphx_glr_lifecycle_008.png" srcset="../_images/sphx_glr_lifecycle_008.png, ../_images/sphx_glr_lifecycle_008_2_00x.png 2.00x" alt="Company Revenue" class = "sphx-glr-single-img"/><p>For labels, we can specify custom formatting guidelines in the form of
functions. Below we define a function that takes an integer as input, and
returns a string as an output. When used with <aclass="reference internal" href="../api/_as_gen/matplotlib.axis.Axis.set_major_formatter.html#matplotlib.axis.Axis.set_major_formatter" title="matplotlib.axis.Axis.set_major_formatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axis.set_major_formatter</span></code></a> or
<aclass="reference internal" href="../api/_as_gen/matplotlib.axis.Axis.set_minor_formatter.html#matplotlib.axis.Axis.set_minor_formatter" title="matplotlib.axis.Axis.set_minor_formatter"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Axis.set_minor_formatter</span></code></a>, they will automatically create and use a
<p>For this function, the <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> argument is the original tick label and <codeclass="docutils literal notranslate"><spanclass="pre">pos</span></code>
is the tick position. We will only use <codeclass="docutils literal notranslate"><spanclass="pre">x</span></code> here but both arguments are
<imgsrc="../_images/sphx_glr_lifecycle_009.png" srcset="../_images/sphx_glr_lifecycle_009.png, ../_images/sphx_glr_lifecycle_009_2_00x.png 2.00x" alt="Company Revenue" class = "sphx-glr-single-img"/></section>
<sectionid="combining-multiple-visualizations">
<h2>Combining multiple visualizations<aclass="headerlink" href="#combining-multiple-visualizations" title="Link to this heading">#</a></h2>
<p>It is possible to draw multiple plot elements on the same instance of
<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.Axes</span></code></a>. To do this we simply need to call another one of
<p>We can then use the <aclass="reference internal" href="../api/figure_api.html#matplotlib.figure.Figure.savefig" title="matplotlib.figure.Figure.savefig"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">figure.Figure.savefig()</span></code></a> in order to save the figure
to disk. Note that there are several useful flags we show below:</p>
<ulclass="simple">
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">transparent=True</span></code> makes the background of the saved figure transparent
if the format supports it.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">dpi=80</span></code> controls the resolution (dots per square inch) of the output.</p></li>
<li><p><codeclass="docutils literal notranslate"><spanclass="pre">bbox_inches="tight"</span></code> fits the bounds of the figure to our plot.</p></li>
</ul>
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Uncomment this line to save the figure.</span>
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#a-note-on-the-explicit-vs-implicit-interfaces">A note on the explicit vs. implicit interfaces</a></li>