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
<pclass="last">Click <aclass="reference internal" href="#sphx-glr-download-tutorials-intermediate-gridspec-py"><spanclass="std std-ref">here</span></a> to download the full example code</p>
<spanid="sphx-glr-tutorials-intermediate-gridspec-py"></span><h1>Customizing Figure Layouts Using GridSpec and Other Functions<aclass="headerlink" href="#customizing-figure-layouts-using-gridspec-and-other-functions" title="Permalink to this headline">¶</a></h1>
<p>How to create grid-shaped combinations of axes.</p>
<blockquote>
<div><dlclass="docutils">
<dt><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">subplots()</span></code></a></dt><dd>Perhaps the primary function used to create figures and axes.
It's also similar to <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.subplot.html#matplotlib.pyplot.subplot" title="matplotlib.pyplot.subplot"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">matplotlib.pyplot.subplot()</span></code></a>,
but creates and places all axes on the figure at once. See also
<dt><aclass="reference internal" href="../../api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec" title="matplotlib.gridspec.GridSpec"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">GridSpec</span></code></a></dt><dd>Specifies the geometry of the grid that a subplot will be
placed. The number of rows and number of columns of the grid
need to be set. Optionally, the subplot layout parameters
(e.g., left, right, etc.) can be tuned.</dd>
<dt><aclass="reference internal" href="../../api/_as_gen/matplotlib.gridspec.SubplotSpec.html#matplotlib.gridspec.SubplotSpec" title="matplotlib.gridspec.SubplotSpec"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">SubplotSpec</span></code></a></dt><dd>Specifies the location of the subplot in the given <em>GridSpec</em>.</dd>
<dt><aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.subplot2grid.html#matplotlib.pyplot.subplot2grid" title="matplotlib.pyplot.subplot2grid"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">subplot2grid()</span></code></a></dt><dd>A helper function that is similar to
It returns a <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> instance and an array of
<p>For a simple use case such as this, <aclass="reference internal" href="../../api/gridspec_api.html#module-matplotlib.gridspec" title="matplotlib.gridspec"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">gridspec</span></code></a> is
perhaps overly verbose.
You have to create the figure and <aclass="reference internal" href="../../api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec" title="matplotlib.gridspec.GridSpec"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">GridSpec</span></code></a>
instance separately, then pass elements of gridspec instance to the
<aclass="reference internal" href="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.add_subplot" title="matplotlib.figure.Figure.add_subplot"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">add_subplot()</span></code></a> method to create the axes
objects.
The elements of the gridspec are accessed in generally the same manner as
syntax for selecting the part of the gridspec each subplot will occupy.</p>
<p>Note that we have also used the convenience method <aclass="reference internal" href="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.add_gridspec" title="matplotlib.figure.Figure.add_gridspec"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Figure.add_gridspec</span></code></a>
instead of <aclass="reference internal" href="../../api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec" title="matplotlib.gridspec.GridSpec"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">gridspec.GridSpec</span></code></a>, potentially saving the user an import,
<p><aclass="reference internal" href="../../api/gridspec_api.html#module-matplotlib.gridspec" title="matplotlib.gridspec"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">gridspec</span></code></a> is also indispensable for creating subplots
of different widths via a couple of methods.</p>
<p>The method shown here is similar to the one above and initializes a
uniform grid specification,
and then uses numpy indexing and slices to allocate multiple
<p>Another option is to use the <codeclass="docutils literal notranslate"><spanclass="pre">width_ratios</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">height_ratios</span></code>
parameters. These keyword arguments are lists of numbers.
Note that absolute values are meaningless, only their relative ratios
matter. That means that <codeclass="docutils literal notranslate"><spanclass="pre">width_ratios=[2,</span><spanclass="pre">4,</span><spanclass="pre">8]</span></code> is equivalent to
<codeclass="docutils literal notranslate"><spanclass="pre">width_ratios=[1,</span><spanclass="pre">2,</span><spanclass="pre">4]</span></code> within equally wide figures.
For the sake of demonstration, we'll blindly create the axes within
<codeclass="docutils literal notranslate"><spanclass="pre">for</span></code> loops since we won't need them later.</p>
<p>Learning to use <codeclass="docutils literal notranslate"><spanclass="pre">width_ratios</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">height_ratios</span></code> is particularly
useful since the top-level function <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">subplots()</span></code></a>
accepts them within the <codeclass="docutils literal notranslate"><spanclass="pre">gridspec_kw</span></code> parameter.
For that matter, any parameter accepted by
<aclass="reference internal" href="../../api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec" title="matplotlib.gridspec.GridSpec"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">GridSpec</span></code></a> can be passed to
<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">subplots()</span></code></a> via the <codeclass="docutils literal notranslate"><spanclass="pre">gridspec_kw</span></code> parameter.
This example recreates the previous figure without directly using a
<p>The <codeclass="docutils literal notranslate"><spanclass="pre">subplots</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">get_gridspec</span></code> methods can be combined since it is
sometimes more convenient to make most of the subplots using <codeclass="docutils literal notranslate"><spanclass="pre">subplots</span></code>
and then remove some and combine them. Here we create a layout with
the bottom two axes in the last column combined.</p>
<h2>Fine Adjustments to a Gridspec Layout<aclass="headerlink" href="#fine-adjustments-to-a-gridspec-layout" title="Permalink to this headline">¶</a></h2>
<p>When a GridSpec is explicitly used, you can adjust the layout
parameters of subplots that are created from the GridSpec. Note this
option is not compatible with <codeclass="docutils literal notranslate"><spanclass="pre">constrained_layout</span></code> or
<aclass="reference internal" href="../../api/_as_gen/matplotlib.figure.Figure.html#matplotlib.figure.Figure.tight_layout" title="matplotlib.figure.Figure.tight_layout"><codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">Figure.tight_layout</span></code></a> which both adjust subplot sizes to fill the
<p>This is similar to <aclass="reference internal" href="../../api/_as_gen/matplotlib.pyplot.subplots_adjust.html#matplotlib.pyplot.subplots_adjust" title="matplotlib.pyplot.subplots_adjust"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">subplots_adjust()</span></code></a>, but it only
affects the subplots that are created from the given GridSpec.</p>
<p>For example, compare the left and right sides of this figure:</p>
<h2>GridSpec using SubplotSpec<aclass="headerlink" href="#gridspec-using-subplotspec" title="Permalink to this headline">¶</a></h2>
<p>You can create GridSpec from the <aclass="reference internal" href="../../api/_as_gen/matplotlib.gridspec.SubplotSpec.html#matplotlib.gridspec.SubplotSpec" title="matplotlib.gridspec.SubplotSpec"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">SubplotSpec</span></code></a>,
in which case its layout parameters are set to that of the location of
the given SubplotSpec.</p>
<p>Note this is also available from the more verbose
<h2>A Complex Nested GridSpec using SubplotSpec<aclass="headerlink" href="#a-complex-nested-gridspec-using-subplotspec" title="Permalink to this headline">¶</a></h2>
<p>Here's a more sophisticated example of nested GridSpec where we put
a box around each cell of the outer 4x4 grid, by hiding appropriate