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
40
41
# It returns a :class:`~matplotlib.figure.Figure` instance and an array of
41
42
# :class:`~matplotlib.axes.Axes` objects.
42
43
43
- fig1 , f1_axes = plt .subplots (ncols = 2 , nrows = 2 )
44
- fig1 .tight_layout ()
44
+ fig1 , f1_axes = plt .subplots (ncols = 2 , nrows = 2 , contstrained_layout = True )
45
45
46
46
############################################################################
47
47
# For a simple use case such as this, :mod:`~matplotlib.gridspec` is
53
53
# The elements of the gridspec are accessed in generally the same manner as
54
54
# numpy arrays.
55
55
56
- fig2 = plt .figure ()
56
+ fig2 = plt .figure (contstrained_layout = True )
57
57
spec2 = gridspec .GridSpec (ncols = 2 , nrows = 2 )
58
58
f2_ax1 = fig2 .add_subplot (spec2 [0 , 0 ])
59
59
f2_ax2 = fig2 .add_subplot (spec2 [0 , 1 ])
60
60
f2_ax3 = fig2 .add_subplot (spec2 [1 , 0 ])
61
61
f2_ax4 = fig2 .add_subplot (spec2 [1 , 1 ])
62
- fig2 .tight_layout ()
63
62
64
63
#############################################################################
65
64
# When you want to have subplots of different sizes, however,
69
68
# and then uses typical numpy indexing and slices to allocate multiple
70
69
# "cells" for a given subplot.
71
70
72
- fig3 = plt .figure ()
73
- spec3 = gridspec . GridSpec (ncols = 3 , nrows = 3 )
71
+ fig3 = plt .figure (contstrained_layout = True )
72
+ spec3 = fig3 . add_gridspec (ncols = 3 , nrows = 3 )
74
73
anno_opts = dict (xy = (0.5 , 0.5 ), xycoords = 'axes fraction' ,
75
74
va = 'center' , ha = 'center' )
76
75
77
76
fig3 .add_subplot (spec3 [0 , 0 ]).annotate ('GridSpec[0, 0]' , ** anno_opts )
78
77
fig3 .add_subplot (spec3 [0 , 1 :]).annotate ('GridSpec[0, 1:]' , ** anno_opts )
79
78
fig3 .add_subplot (spec3 [1 :, 0 ]).annotate ('GridSpec[1:, 0]' , ** anno_opts )
80
79
fig3 .add_subplot (spec3 [1 :, 1 :]).annotate ('GridSpec[1:, 1:]' , ** anno_opts )
81
-
82
- fig3 .tight_layout ()
80
+ fig3 . canvas . draw () # Sometime constrained_layout needs an extra draw...
81
+ fig3 .show ()
83
82
84
83
############################################################################
85
84
# Other option is to use the ``width_ratios`` and ``height_ratios``
90
89
# For the sake of demonstration, we'll blindly create the axes within
91
90
# ``for`` loops since we won't need them later.
92
91
93
- fig4 = plt .figure ()
92
+ fig4 = plt .figure (contstrained_layout = True )
94
93
widths = [2 , 3 , 1.5 ]
95
94
heights = [1 , 3 , 2 ]
96
- spec4 = gridspec . GridSpec (ncols = 3 , nrows = 3 , width_ratios = widths ,
95
+ spec4 = fig4 . add_gridspec (ncols = 3 , nrows = 3 , width_ratios = widths ,
97
96
height_ratios = heights )
98
97
for row in range (3 ):
99
98
for col in range (3 ):
100
99
ax = fig4 .add_subplot (spec4 [row , col ])
101
100
label = 'Width: {}\n Height: {}' .format (widths [col ], heights [row ])
102
101
ax .annotate (label , (0.1 , 0.5 ), xycoords = 'axes fraction' , va = 'center' )
103
102
104
- fig4 .tight_layout ()
105
-
106
103
############################################################################
107
104
# Learning to use ``width_ratios`` and ``height_ratios`` is particularly
108
105
# useful since the top-level function :func:`~matplotlib.pyplot.subplots`
114
111
# gridspec instance.
115
112
116
113
gs_kw = dict (width_ratios = widths , height_ratios = heights )
117
- fig5 , f5_axes = plt .subplots (ncols = 3 , nrows = 3 , gridspec_kw = gs_kw )
114
+ fig5 , f5_axes = plt .subplots (ncols = 3 , nrows = 3 , contstrained_layout = True ,
115
+ gridspec_kw = gs_kw )
118
116
for r , row in enumerate (f5_axes ):
119
117
for c , ax in enumerate (row ):
120
118
label = 'Width: {}\n Height: {}' .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
# ==========================
165
160
# in which case its layout parameters are set to that of the location of
166
161
# the given SubplotSpec.
167
162
168
- fig = plt .figure ()
169
- gs0 = gridspec . GridSpec (1 , 2 )
163
+ fig = plt .figure (contstrained_layout = True )
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
for a in range (2 ):
175
170
for b in range (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
import numpy as np
190
183
from itertools import product
191
184
192
-
193
185
def squiggle_xy (a , b , c , d , i = np .arange (0.0 , 2 * np .pi , 0.05 )):
194
186
return np .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
for i in range (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
for j , (c , d ) in enumerate (product (range (1 , 4 ), repeat = 2 )):
206
197
ax = plt .Subplot (fig , inner_grid [j ])
@@ -225,3 +216,19 @@ def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
225
216
ax .spines ['right' ].set_visible (True )
226
217
227
218
plt .show ()
219
+
220
+ #############################################################################
221
+ #
222
+ # ------------
223
+ #
224
+ # References
225
+ # """"""""""
226
+ #
227
+ # The usage of the following functions and methods is shown in this example:
228
+
229
+ matplotlib .pyplot .subplots
230
+ matplotlib .figure .Figure .add_gridspec
231
+ matplotlib .figure .Figure .add_subplot
232
+ matplotlib .gridspec .GridSpec
233
+ matplotlib .gridspec .SubplotSpec .subgridspec
234
+ matplotlib .gridspec .GridSpecFromSubplotSpec
0 commit comments