|
7 | 7 |
|
8 | 8 | :func:`~matplotlib.pyplot.subplots`
|
9 | 9 | Perhaps the primary function used to create figures and axes.
|
10 |
| - It's also similar to :func:`~matplotlib.pyplot.subplot`, |
11 |
| - but creates and places all axes on the figure at once. |
| 10 | + It's also similar to :func:`.matplotlib.pyplot.subplot`, |
| 11 | + but creates and places all axes on the figure at once. See also |
| 12 | + `.matplotlib.Figure.subplots`. |
12 | 13 |
|
13 | 14 | :class:`~matplotlib.gridspec.GridSpec`
|
14 | 15 | Specifies the geometry of the grid that a subplot will be
|
|
28 | 29 |
|
29 | 30 | importmatplotlib.pyplotasplt
|
30 | 31 | importmatplotlib.gridspecasgridspec
|
| 32 | +plt.rcParams['figure.constrained_layout.use']=True |
31 | 33 |
|
32 | 34 | ############################################################################
|
33 | 35 | # Basic Quickstart Guide
|
|
41 | 43 | # :class:`~matplotlib.axes.Axes` objects.
|
42 | 44 |
|
43 | 45 | fig1,f1_axes=plt.subplots(ncols=2,nrows=2)
|
44 |
| -fig1.tight_layout() |
45 | 46 |
|
46 | 47 | ############################################################################
|
47 | 48 | # For a simple use case such as this, :mod:`~matplotlib.gridspec` is
|
|
54 | 55 | # numpy arrays.
|
55 | 56 |
|
56 | 57 | fig2=plt.figure()
|
57 |
| -spec2=gridspec.GridSpec(ncols=2,nrows=2) |
| 58 | +spec2=fig2.add_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 |
| -fig2.tight_layout() |
63 | 63 |
|
64 | 64 | #############################################################################
|
65 | 65 | # When you want to have subplots of different sizes, however,
|
|
70 | 70 | # "cells" for a given subplot.
|
71 | 71 |
|
72 | 72 | fig3=plt.figure()
|
73 |
| -spec3=gridspec.GridSpec(ncols=3,nrows=3) |
| 73 | +spec3=fig3.add_gridspec(ncols=3,nrows=3) |
74 | 74 | anno_opts=dict(xy=(0.5,0.5),xycoords='axes fraction',
|
75 | 75 | va='center',ha='center')
|
76 | 76 |
|
77 | 77 | fig3.add_subplot(spec3[0,0]).annotate('GridSpec[0, 0]',**anno_opts)
|
78 | 78 | fig3.add_subplot(spec3[0,1:]).annotate('GridSpec[0, 1:]',**anno_opts)
|
79 | 79 | fig3.add_subplot(spec3[1:,0]).annotate('GridSpec[1:, 0]',**anno_opts)
|
80 | 80 | fig3.add_subplot(spec3[1:,1:]).annotate('GridSpec[1:, 1:]',**anno_opts)
|
81 |
| - |
82 |
| -fig3.tight_layout() |
| 81 | +fig3.canvas.draw()# Sometime constrained_layout needs an extra draw... |
| 82 | +fig3.show() |
83 | 83 |
|
84 | 84 | ############################################################################
|
85 | 85 | # Other option is to use the ``width_ratios`` and ``height_ratios``
|
|
93 | 93 | fig4=plt.figure()
|
94 | 94 | widths= [2,3,1.5]
|
95 | 95 | heights= [1,3,2]
|
96 |
| -spec4=gridspec.GridSpec(ncols=3,nrows=3,width_ratios=widths, |
| 96 | +spec4=fig4.add_gridspec(ncols=3,nrows=3,width_ratios=widths, |
97 | 97 | height_ratios=heights)
|
98 | 98 | forrowinrange(3):
|
99 | 99 | forcolinrange(3):
|
100 | 100 | ax=fig4.add_subplot(spec4[row,col])
|
101 | 101 | label='Width: {}\nHeight: {}'.format(widths[col],heights[row])
|
102 | 102 | ax.annotate(label, (0.1,0.5),xycoords='axes fraction',va='center')
|
103 | 103 |
|
104 |
| -fig4.tight_layout() |
105 |
| - |
106 | 104 | ############################################################################
|
107 | 105 | # Learning to use ``width_ratios`` and ``height_ratios`` is particularly
|
108 | 106 | # useful since the top-level function :func:`~matplotlib.pyplot.subplots`
|
|
120 | 118 | label='Width: {}\nHeight: {}'.format(widths[c],heights[r])
|
121 | 119 | ax.annotate(label, (0.1,0.5),xycoords='axes fraction',va='center')
|
122 | 120 |
|
123 |
| -fig5.tight_layout() |
124 |
| - |
125 |
| - |
126 | 121 | ###############################################################################
|
127 | 122 | # Fine Adjustments to a Gridspec Layout
|
128 | 123 | # =====================================
|
129 | 124 | #
|
130 | 125 | # When a GridSpec is explicitly used, you can adjust the layout
|
131 | 126 | # parameters of subplots that are created from the GridSpec.
|
132 | 127 |
|
133 |
| -fig=plt.figure() |
134 |
| -gs1=gridspec.GridSpec(nrows=3,ncols=3,left=0.05,right=0.48,wspace=0.05) |
135 |
| -ax1=fig.add_subplot(gs1[:-1, :]) |
136 |
| -ax2=fig.add_subplot(gs1[-1, :-1]) |
137 |
| -ax3=fig.add_subplot(gs1[-1,-1]) |
138 |
| - |
| 128 | +fig6=plt.figure() |
| 129 | +gs1=fig6.add_gridspec(nrows=3,ncols=3,left=0.05,right=0.48,wspace=0.05) |
| 130 | +ax1=fig6.add_subplot(gs1[:-1, :]) |
| 131 | +ax2=fig6.add_subplot(gs1[-1, :-1]) |
| 132 | +ax3=fig6.add_subplot(gs1[-1,-1]) |
| 133 | +fig6.canvas.draw() |
139 | 134 |
|
140 | 135 | ###############################################################################
|
141 | 136 | # This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
|
142 | 137 | # affects the subplots that are created from the given GridSpec.
|
143 | 138 | #
|
144 | 139 | # For example, compare the left and right sides of this figure:
|
145 | 140 |
|
146 |
| -fig=plt.figure() |
147 |
| -gs1=gridspec.GridSpec(nrows=3,ncols=3,left=0.05,right=0.48, |
| 141 | +fig=plt.figure(constrained_layout=False) |
| 142 | +gs1=fig.add_gridspec(nrows=3,ncols=3,left=0.05,right=0.48, |
148 | 143 | wspace=0.05)
|
149 | 144 | ax1=fig.add_subplot(gs1[:-1, :])
|
150 | 145 | ax2=fig.add_subplot(gs1[-1, :-1])
|
151 | 146 | ax3=fig.add_subplot(gs1[-1,-1])
|
152 | 147 |
|
153 |
| - |
154 |
| -gs2=gridspec.GridSpec(nrows=3,ncols=3,left=0.55,right=0.98, |
| 148 | +gs2=fig.add_gridspec(nrows=3,ncols=3,left=0.55,right=0.98, |
155 | 149 | hspace=0.05)
|
156 | 150 | ax4=fig.add_subplot(gs2[:, :-1])
|
157 | 151 | ax5=fig.add_subplot(gs2[:-1,-1])
|
158 | 152 | ax6=fig.add_subplot(gs2[-1,-1])
|
159 | 153 |
|
| 154 | + |
160 | 155 | ###############################################################################
|
161 | 156 | # GridSpec using SubplotSpec
|
162 | 157 | # ==========================
|
|
166 | 161 | # the given SubplotSpec.
|
167 | 162 |
|
168 | 163 | fig=plt.figure()
|
169 |
| -gs0=gridspec.GridSpec(1,2) |
| 164 | +gs0=fig.add_gridspec(1,2) |
170 | 165 |
|
171 |
| -gs00=gridspec.GridSpecFromSubplotSpec(2,3,subplot_spec=gs0[0]) |
172 |
| -gs01=gridspec.GridSpecFromSubplotSpec(3,2,subplot_spec=gs0[1]) |
| 166 | +gs00=gs0[0].subgridspec(2,3) |
| 167 | +gs01=gs0[1].subgridspec(3,2) |
173 | 168 |
|
174 | 169 | forainrange(2):
|
175 | 170 | forbinrange(3):
|
176 | 171 | fig.add_subplot(gs00[a,b])
|
177 | 172 | fig.add_subplot(gs01[b,a])
|
178 | 173 |
|
179 |
| -fig.tight_layout() |
180 |
| - |
181 | 174 | ###############################################################################
|
182 | 175 | # A Complex Nested GridSpec using SubplotSpec
|
183 | 176 | # ===========================================
|
|
189 | 182 | importnumpyasnp
|
190 | 183 | fromitertoolsimportproduct
|
191 | 184 |
|
192 |
| - |
193 | 185 | defsquiggle_xy(a,b,c,d,i=np.arange(0.0,2*np.pi,0.05)):
|
194 | 186 | returnnp.sin(i*a)*np.cos(i*b),np.sin(i*c)*np.cos(i*d)
|
195 | 187 |
|
196 |
| -fig=plt.figure(figsize=(8,8)) |
| 188 | +fig=plt.figure(figsize=(8,8),constrained_layout=False) |
197 | 189 |
|
198 | 190 | # gridspec inside gridspec
|
199 |
| -outer_grid=gridspec.GridSpec(4,4,wspace=0.0,hspace=0.0) |
| 191 | +outer_grid=fig.add_gridspec(4,4,wspace=0.0,hspace=0.0) |
200 | 192 |
|
201 | 193 | foriinrange(16):
|
202 |
| -inner_grid=gridspec.GridSpecFromSubplotSpec( |
203 |
| -3,3,subplot_spec=outer_grid[i],wspace=0.0,hspace=0.0) |
| 194 | +inner_grid=outer_grid[i].subgridspec(3,3,wspace=0.0,hspace=0.0) |
204 | 195 | a,b=int(i/4)+1,i%4+1
|
205 | 196 | forj, (c,d)inenumerate(product(range(1,4),repeat=2)):
|
206 | 197 | ax=plt.Subplot(fig,inner_grid[j])
|
|