|
27 | 27 |
|
28 | 28 | """
|
29 | 29 |
|
| 30 | +import matplotlib |
30 | 31 | import matplotlib.pyplot as plt
|
31 | 32 | import matplotlib.gridspec as gridspec
|
32 | 33 |
|
33 | 34 | ############################################################################
|
34 | 35 | # Basic Quickstart Guide
|
35 | 36 | # ======================
|
36 | 37 | #
|
37 |
| -# These first two examples show how to create a basic 4-by-4 grid using |
| 38 | +# These first two examples show how to create a basic 2-by-2 grid using |
38 | 39 | # both :func:`~matplotlib.pyplot.subplots` and :mod:`~matplotlib.gridspec`.
|
39 | 40 | #
|
40 | 41 | # Using :func:`~matplotlib.pyplot.subplots` is quite simple.
|
41 | 42 | # It returns a :class:`~matplotlib.figure.Figure` instance and an array of
|
42 | 43 | # :class:`~matplotlib.axes.Axes` objects.
|
43 | 44 |
|
44 |
| -fig1, f1_axes = plt.subplots(ncols=2, nrows=2, contstrained_layout=True) |
| 45 | +fig1, f1_axes = plt.subplots(ncols=2, nrows=2, constrained_layout=True) |
45 | 46 |
|
46 | 47 | ############################################################################
|
47 | 48 | # For a simple use case such as this, :mod:`~matplotlib.gridspec` is
|
|
53 | 54 | # The elements of the gridspec are accessed in generally the same manner as
|
54 | 55 | # numpy arrays.
|
55 | 56 |
|
56 |
| -fig2 = plt.figure(contstrained_layout=True) |
| 57 | +fig2 = plt.figure(constrained_layout=True) |
57 | 58 | spec2 = gridspec.GridSpec(ncols=2, nrows=2)
|
58 | 59 | f2_ax1 = fig2.add_subplot(spec2[0, 0])
|
59 | 60 | f2_ax2 = fig2.add_subplot(spec2[0, 1])
|
60 | 61 | f2_ax3 = fig2.add_subplot(spec2[1, 0])
|
61 | 62 | f2_ax4 = fig2.add_subplot(spec2[1, 1])
|
62 | 63 |
|
63 | 64 | #############################################################################
|
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 |
| 65 | +# The power of gridspec comes in being able to create subplots that span |
| 66 | +# rows and columns. Note the |
| 67 | +# `Numpy slice <https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html>`_ |
| 68 | +# syntax for selecing the part of the gridspec each subplot will occupy. |
| 69 | +# |
| 70 | +# Note that we have also used the convenience method `.Figure.add_grisdpec` |
| 71 | +# instead of `.gridspec.GridSpec`, potentially saving the user an import, |
| 72 | +# and keeping the namespace cleaner. |
| 73 | + |
| 74 | +fig = plt.figure(constrained_layout=True) |
| 75 | +gs = fig.add_gridspec(3, 3) |
| 76 | +ax1 = fig.add_subplot(gs[0, :]) |
| 77 | +ax1.set_title('gs[0, :]') |
| 78 | +ax2 = fig.add_subplot(gs[1, :-1]) |
| 79 | +ax2.set_title('gs[1, :-1]') |
| 80 | +ax3 = fig.add_subplot(gs[1:, -1]) |
| 81 | +ax3.set_title('gs[1:, -1]') |
| 82 | +ax4 = fig.add_subplot(gs[-1, 0]) |
| 83 | +ax4.set_title('gs[-1, 0]') |
| 84 | +ax5 = fig.add_subplot(gs[-1, -2]) |
| 85 | +ax5.set_title('gs[-1, -2]') |
| 86 | + |
| 87 | +############################################################################# |
| 88 | +# :mod:`~matplotlib.gridspec` is also indispensable for creating subplots |
| 89 | +# of different widths via a couple of methods. |
| 90 | +# |
| 91 | +# The method shown here is similar to the one above and initializes a |
| 92 | +# uniform grid specification, |
| 93 | +# and then uses numpy indexing and slices to allocate multiple |
69 | 94 | # "cells" for a given subplot.
|
70 | 95 |
|
71 |
| -fig3 = plt.figure(contstrained_layout=True) |
| 96 | +fig3 = plt.figure(constrained_layout=True) |
72 | 97 | spec3 = fig3.add_gridspec(ncols=3, nrows=3)
|
73 | 98 | anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',
|
74 | 99 | va='center', ha='center')
|
75 | 100 |
|
76 |
| -fig3.add_subplot(spec3[0, 0]).annotate('GridSpec[0, 0]', **anno_opts) |
| 101 | +ax1 = fig3.add_subplot(spec3[0, 0]) |
| 102 | +ax1.annotate('GridSpec[0, 0]', **anno_opts) |
77 | 103 | fig3.add_subplot(spec3[0, 1:]).annotate('GridSpec[0, 1:]', **anno_opts)
|
78 | 104 | fig3.add_subplot(spec3[1:, 0]).annotate('GridSpec[1:, 0]', **anno_opts)
|
79 | 105 | 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 | 106 |
|
83 | 107 | ############################################################################
|
84 |
| -# Other option is to use the ``width_ratios`` and ``height_ratios`` |
| 108 | +# Another option is to use the ``width_ratios`` and ``height_ratios`` |
85 | 109 | # parameters. These keyword arguments are lists of numbers.
|
86 | 110 | # Note that absolute values are meaningless, only their relative ratios
|
87 | 111 | # matter. That means that ``width_ratios=[2, 4, 8]`` is equivalent to
|
88 | 112 | # ``width_ratios=[1, 2, 4]`` within equally wide figures.
|
89 | 113 | # For the sake of demonstration, we'll blindly create the axes within
|
90 | 114 | # ``for`` loops since we won't need them later.
|
91 | 115 |
|
92 |
| -fig4 = plt.figure(contstrained_layout=True) |
| 116 | +fig4 = plt.figure(constrained_layout=True) |
93 | 117 | widths = [2, 3, 1.5]
|
94 | 118 | heights = [1, 3, 2]
|
95 | 119 | spec4 = fig4.add_gridspec(ncols=3, nrows=3, width_ratios=widths,
|
|
111 | 135 | # gridspec instance.
|
112 | 136 |
|
113 | 137 | gs_kw = dict(width_ratios=widths, height_ratios=heights)
|
114 |
| -fig5, f5_axes = plt.subplots(ncols=3, nrows=3, contstrained_layout=True, |
| 138 | +fig5, f5_axes = plt.subplots(ncols=3, nrows=3, constrained_layout=True, |
115 | 139 | gridspec_kw=gs_kw)
|
116 | 140 | for r, row in enumerate(f5_axes):
|
117 | 141 | for c, ax in enumerate(row):
|
|
123 | 147 | # =====================================
|
124 | 148 | #
|
125 | 149 | # When a GridSpec is explicitly used, you can adjust the layout
|
126 |
| -# parameters of subplots that are created from the GridSpec. |
| 150 | +# parameters of subplots that are created from the GridSpec. Note this |
| 151 | +# option is not compatible with ``constrained_layout`` or |
| 152 | +# `.Figure.tight_layout` which both adjust subplot sizes to fill the |
| 153 | +# figure. |
127 | 154 |
|
128 |
| -fig6 = plt.figure() |
| 155 | +fig6 = plt.figure(constrained_layout=False) |
129 | 156 | gs1 = fig6.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
|
130 | 157 | ax1 = fig6.add_subplot(gs1[:-1, :])
|
131 | 158 | ax2 = fig6.add_subplot(gs1[-1, :-1])
|
132 | 159 | ax3 = fig6.add_subplot(gs1[-1, -1])
|
133 |
| -fig6.canvas.draw() |
134 | 160 |
|
135 | 161 | ###############################################################################
|
136 | 162 | # This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
|
137 | 163 | # affects the subplots that are created from the given GridSpec.
|
138 | 164 | #
|
139 | 165 | # For example, compare the left and right sides of this figure:
|
140 | 166 |
|
141 |
| -fig = plt.figure(constrained_layout=False) |
142 |
| -gs1 = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, |
| 167 | +fig7 = plt.figure(constrained_layout=False) |
| 168 | +gs1 = fig7.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, |
143 | 169 | 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]) |
| 170 | +ax1 = fig7.add_subplot(gs1[:-1, :]) |
| 171 | +ax2 = fig7.add_subplot(gs1[-1, :-1]) |
| 172 | +ax3 = fig7.add_subplot(gs1[-1, -1]) |
147 | 173 |
|
148 |
| -gs2 = fig.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98, |
| 174 | +gs2 = fig7.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98, |
149 | 175 | 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]) |
| 176 | +ax4 = fig7.add_subplot(gs2[:, :-1]) |
| 177 | +ax5 = fig7.add_subplot(gs2[:-1, -1]) |
| 178 | +ax6 = fig7.add_subplot(gs2[-1, -1]) |
153 | 179 |
|
154 | 180 |
|
155 | 181 | ###############################################################################
|
|
159 | 185 | # You can create GridSpec from the :class:`~matplotlib.gridspec.SubplotSpec`,
|
160 | 186 | # in which case its layout parameters are set to that of the location of
|
161 | 187 | # the given SubplotSpec.
|
| 188 | +# |
| 189 | +# Note this is also available from the more verbose |
| 190 | +# `.gridspec.GridSpecFromSubplotSpec`. |
162 | 191 |
|
163 |
| -fig = plt.figure(contstrained_layout=True) |
| 192 | +fig = plt.figure(constrained_layout=True) |
164 | 193 | gs0 = fig.add_gridspec(1, 2)
|
165 | 194 |
|
166 | 195 | gs00 = gs0[0].subgridspec(2, 3)
|
|
0 commit comments