<pclass="last">Click <aclass="reference internal" href="#sphx-glr-download-tutorials-introductory-lifecycle-py"><spanclass="std std-ref">here</span></a> to download the full example code</p>
<spanid="sphx-glr-tutorials-introductory-lifecycle-py"></span><h1>The Lifecycle of a Plot<aclass="headerlink" href="#the-lifecycle-of-a-plot" title="Permalink to this headline">¶</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'll try
to highlight some neat features and best-practices using Matplotlib.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">This tutorial is based off of
<aclass="reference external" href="http://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 Object-Oriented API vs. Pyplot<aclass="headerlink" href="#a-note-on-the-object-oriented-api-vs-pyplot" title="Permalink to this headline">¶</a></h2>
<p>Matplotlib has two interfaces. The first is an object-oriented (OO)
interface. In this case, we utilize an instance 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.Axes</span></code></a>
in order to render visualizations on an instance of <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.Figure</span></code></a>.</p>
<p>The second is based on MATLAB and uses a state-based interface. This is
encapsulated 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">pyplot</span></code></a> module. See the <aclass="reference internal" href="pyplot.html"><spanclass="doc">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>The Figure is the final image that may contain 1 or more Axes.</li>
<li>The Axes represent an individual plot (don't confuse this with the word
"axis", which refers to the x/y axis of a plot).</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="first admonition-title">Note</p>
<pclass="last">In general, try to use the object-oriented interface over the pyplot
interface.</p>
</div>
</div>
<divclass="section" id="our-data">
<h2>Our data<aclass="headerlink" href="#our-data" title="Permalink to this headline">¶</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="Permalink to this headline">¶</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'll first generate
an instance of <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.Figure</span></code></a> and
<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.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="first admonition-title">Note</p>
<pclass="last">Figures can have multiple axes on them. For information on how to do this,
see the <aclass="reference internal" href="../intermediate/tight_layout_guide.html"><spanclass="doc">Tight Layout tutorial</span></a>.</p>
<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>
<p>Next, we'll add labels to the plot. To do this with the OO interface,
we can use the <aclass="reference internal" href="../../api/_as_gen/matplotlib.axes.Axes.set.html#matplotlib.axes.Axes.set" title="matplotlib.axes.Axes.set"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">axes.Axes.set()</span></code></a> method to set properties of this
<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 <codeclass="docutils literal notranslate"><spanclass="pre">figsize</span></code> kwarg.</p>
<divclass="admonition note">
<pclass="first admonition-title">Note</p>
<pclass="last">While indexing in NumPy follows the form (row, column), the figsize
kwarg follows the form (width, height). This follows conventions in
visualization, which unfortunately are different from those of linear
<h2>Combining multiple visualizations<aclass="headerlink" href="#combining-multiple-visualizations" title="Permalink to this headline">¶</a></h2>
<p>It is possible to draw multiple plot elements on the same instance 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.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/_as_gen/matplotlib.figure.Figure.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'll show below:</p>
<ulclass="simple">
<li><codeclass="docutils literal notranslate"><spanclass="pre">transparent=True</span></code> makes the background of the saved figure transparent
if the format supports it.</li>
<li><codeclass="docutils literal notranslate"><spanclass="pre">dpi=80</span></code> controls the resolution (dots per square inch) of the output.</li>
<li><codeclass="docutils literal notranslate"><spanclass="pre">bbox_inches="tight"</span></code> fits the bounds of the figure to our plot.</li>
</ul>
<divclass="highlight-default notranslate"><divclass="highlight"><pre><span></span><spanclass="c1"># Uncomment this line to save the figure.</span>