|
34 | 34 | # Basic Quickstart Guide
|
35 | 35 | # ======================
|
36 | 36 | #
|
37 |
| -# These first two examples show how to create a basic 4-by-4 grid using |
| 37 | +# These first two examples show how to create a basic 2-by-2 grid using |
38 | 38 | # both :func:`~matplotlib.pyplot.subplots` and :mod:`~matplotlib.gridspec`.
|
39 | 39 | #
|
40 | 40 | # Using :func:`~matplotlib.pyplot.subplots` is quite simple.
|
|
61 | 61 | f2_ax4 = fig2.add_subplot(spec2[1, 1])
|
62 | 62 |
|
63 | 63 | #############################################################################
|
64 |
| -# When you want to have subplots of different sizes, however, |
65 |
| -# :mod:`~matplotlib.gridspec` becomes indispensable and provides a couple |
66 |
| -# of options. |
67 |
| -# The method shown here initializes a uniform grid specification, |
68 |
| -# and then uses typical numpy indexing and slices to allocate multiple |
| 64 | +# The power of gridspec comes in being able to create subplots that span |
| 65 | +# rows and columns. Note the |
| 66 | +# `Numpy slice <https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_ |
| 67 | +# syntax for selecing the part of the gridspec each subplot will occupy. |
| 68 | +# |
| 69 | +# Note that we have also used the convenience method `.Figure.add_grisdpec` |
| 70 | +# instead of `.gridspec.GridSpec`, potentially saving the user an import, |
| 71 | +# and keeping the namespace cleaner. |
| 72 | + |
| 73 | +fig = plt.figure() |
| 74 | +gs = fig.add_gridspec(3, 3) |
| 75 | +ax1 = fig.add_subplot(gs[0, :]) |
| 76 | +ax1.set_title('gs[0, :]') |
| 77 | +ax2 = fig.add_subplot(gs[1, :-1]) |
| 78 | +ax2.set_title('gs[1, :-1]') |
| 79 | +ax3 = fig.add_subplot(gs[1:, -1]) |
| 80 | +ax3.set_title('gs[1:, -1]') |
| 81 | +ax4 = fig.add_subplot(gs[-1, 0]) |
| 82 | +ax4.set_title('gs[-1, 0]') |
| 83 | +ax5 = fig.add_subplot(gs[-1, -2]) |
| 84 | +ax5.set_title('gs[-1, -2]') |
| 85 | + |
| 86 | +############################################################################# |
| 87 | +# :mod:`~matplotlib.gridspec` is also indispensable for creating subplots |
| 88 | +# of different widths via a couple of methods. |
| 89 | +# |
| 90 | +# The method shown here is similar to the one above and initializes a |
| 91 | +# uniform grid specification, |
| 92 | +# and then uses numpy indexing and slices to allocate multiple |
69 | 93 | # "cells" for a given subplot.
|
70 | 94 |
|
71 | 95 | fig3 = plt.figure(contstrained_layout=True)
|
72 | 96 | spec3 = fig3.add_gridspec(ncols=3, nrows=3)
|
73 | 97 | anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',
|
74 | 98 | va='center', ha='center')
|
75 | 99 |
|
76 |
| -fig3.add_subplot(spec3[0, 0]).annotate('GridSpec[0, 0]', **anno_opts) |
| 100 | +ax1 = fig3.add_subplot(spec3[0, 0]) |
| 101 | +ax1.annotate('GridSpec[0, 0]', **anno_opts) |
77 | 102 | fig3.add_subplot(spec3[0, 1:]).annotate('GridSpec[0, 1:]', **anno_opts)
|
78 | 103 | fig3.add_subplot(spec3[1:, 0]).annotate('GridSpec[1:, 0]', **anno_opts)
|
79 | 104 | fig3.add_subplot(spec3[1:, 1:]).annotate('GridSpec[1:, 1:]', **anno_opts)
|
80 |
| -fig3.canvas.draw() # Sometime constrained_layout needs an extra draw... |
81 |
| -fig3.show() |
82 | 105 |
|
83 | 106 | ############################################################################
|
84 |
| -# Other option is to use the ``width_ratios`` and ``height_ratios`` |
| 107 | +# Another option is to use the ``width_ratios`` and ``height_ratios`` |
85 | 108 | # parameters. These keyword arguments are lists of numbers.
|
86 | 109 | # Note that absolute values are meaningless, only their relative ratios
|
87 | 110 | # matter. That means that ``width_ratios=[2, 4, 8]`` is equivalent to
|
|
123 | 146 | # =====================================
|
124 | 147 | #
|
125 | 148 | # When a GridSpec is explicitly used, you can adjust the layout
|
126 |
| -# parameters of subplots that are created from the GridSpec. |
| 149 | +# parameters of subplots that are created from the GridSpec. Note this |
| 150 | +# option is not compatible with ``constrained_layout`` or |
| 151 | +# `.Figure.tight_layout` which both adjust subplot sizes to fill the |
| 152 | +# figure. |
127 | 153 |
|
128 |
| -fig6 = plt.figure() |
| 154 | +fig6 = plt.figure(constrained_layout=False) |
129 | 155 | gs1 = fig6.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
|
130 | 156 | ax1 = fig6.add_subplot(gs1[:-1, :])
|
131 | 157 | ax2 = fig6.add_subplot(gs1[-1, :-1])
|
132 | 158 | ax3 = fig6.add_subplot(gs1[-1, -1])
|
133 |
| -fig6.canvas.draw() |
134 | 159 |
|
135 | 160 | ###############################################################################
|
136 | 161 | # This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
|
137 | 162 | # affects the subplots that are created from the given GridSpec.
|
138 | 163 | #
|
139 | 164 | # For example, compare the left and right sides of this figure:
|
140 | 165 |
|
141 |
| -fig = plt.figure(constrained_layout=False) |
142 |
| -gs1 = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, |
| 166 | +fig7 = plt.figure(constrained_layout=False) |
| 167 | +gs1 = fig7.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, |
143 | 168 | wspace=0.05)
|
144 |
| -ax1 = fig.add_subplot(gs1[:-1, :]) |
145 |
| -ax2 = fig.add_subplot(gs1[-1, :-1]) |
146 |
| -ax3 = fig.add_subplot(gs1[-1, -1]) |
| 169 | +ax1 = fig7.add_subplot(gs1[:-1, :]) |
| 170 | +ax2 = fig7.add_subplot(gs1[-1, :-1]) |
| 171 | +ax3 = fig7.add_subplot(gs1[-1, -1]) |
147 | 172 |
|
148 |
| -gs2 = fig.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98, |
| 173 | +gs2 = fig7.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98, |
149 | 174 | hspace=0.05)
|
150 |
| -ax4 = fig.add_subplot(gs2[:, :-1]) |
151 |
| -ax5 = fig.add_subplot(gs2[:-1, -1]) |
152 |
| -ax6 = fig.add_subplot(gs2[-1, -1]) |
| 175 | +ax4 = fig7.add_subplot(gs2[:, :-1]) |
| 176 | +ax5 = fig7.add_subplot(gs2[:-1, -1]) |
| 177 | +ax6 = fig7.add_subplot(gs2[-1, -1]) |
153 | 178 |
|
154 | 179 |
|
155 | 180 | ###############################################################################
|
|
159 | 184 | # You can create GridSpec from the :class:`~matplotlib.gridspec.SubplotSpec`,
|
160 | 185 | # in which case its layout parameters are set to that of the location of
|
161 | 186 | # the given SubplotSpec.
|
| 187 | +# |
| 188 | +# Note this is also available from the more verbose |
| 189 | +# `.gridspec.GridSpecFromSubplotSpec`. |
162 | 190 |
|
163 | 191 | fig = plt.figure(contstrained_layout=True)
|
164 | 192 | gs0 = fig.add_gridspec(1, 2)
|
|
0 commit comments