|
49 | 49 | # For a simple use case such as this, :mod:`~matplotlib.gridspec` is
|
50 | 50 | # perhaps overly verbose.
|
51 | 51 | # You have to create the figure and :class:`~matplotlib.gridspec.GridSpec`
|
52 |
| -# instance separately, then pass elements of gridspec instance to the |
53 |
| -# :func:`~matplotlib.figure.Figure.add_subplot` method to create the axes |
54 |
| -# objects. |
| 52 | +# instance separately, then call the `~SubplotSpec.add_subplot` method of |
| 53 | +# elements of gridspec instance to create the axes objects. |
55 | 54 | # The elements of the gridspec are accessed in generally the same manner as
|
56 | 55 | # numpy arrays.
|
57 | 56 |
|
58 | 57 | fig2 = plt.figure(constrained_layout=True)
|
59 | 58 | spec2 = gridspec.GridSpec(ncols=2, nrows=2, figure=fig2)
|
60 |
| -f2_ax1 = fig2.add_subplot(spec2[0, 0]) |
61 |
| -f2_ax2 = fig2.add_subplot(spec2[0, 1]) |
62 |
| -f2_ax3 = fig2.add_subplot(spec2[1, 0]) |
63 |
| -f2_ax4 = fig2.add_subplot(spec2[1, 1]) |
| 59 | +f2_ax1 = spec2[0, 0].add_subplot() |
| 60 | +f2_ax2 = spec2[0, 1].add_subplot() |
| 61 | +f2_ax3 = spec2[1, 0].add_subplot() |
| 62 | +f2_ax4 = spec2[1, 1].add_subplot() |
64 | 63 |
|
65 | 64 | #############################################################################
|
66 | 65 | # The power of gridspec comes in being able to create subplots that span
|
|
74 | 73 |
|
75 | 74 | fig3 = plt.figure(constrained_layout=True)
|
76 | 75 | gs = fig3.add_gridspec(3, 3)
|
77 |
| -f3_ax1 = fig3.add_subplot(gs[0, :]) |
| 76 | +f3_ax1 = gs[0, :].add_subplot() |
78 | 77 | f3_ax1.set_title('gs[0, :]')
|
79 |
| -f3_ax2 = fig3.add_subplot(gs[1, :-1]) |
| 78 | +f3_ax2 = gs[1, :-1].add_subplot() |
80 | 79 | f3_ax2.set_title('gs[1, :-1]')
|
81 |
| -f3_ax3 = fig3.add_subplot(gs[1:, -1]) |
| 80 | +f3_ax3 = gs[1:, -1].add_subplot() |
82 | 81 | f3_ax3.set_title('gs[1:, -1]')
|
83 |
| -f3_ax4 = fig3.add_subplot(gs[-1, 0]) |
| 82 | +f3_ax4 = gs[-1, 0].add_subplot() |
84 | 83 | f3_ax4.set_title('gs[-1, 0]')
|
85 |
| -f3_ax5 = fig3.add_subplot(gs[-1, -2]) |
| 84 | +f3_ax5 = gs[-1, -2].add_subplot() |
86 | 85 | f3_ax5.set_title('gs[-1, -2]')
|
87 | 86 |
|
88 | 87 | #############################################################################
|
|
99 | 98 | anno_opts = dict(xy=(0.5, 0.5), xycoords='axes fraction',
|
100 | 99 | va='center', ha='center')
|
101 | 100 |
|
102 |
| -f4_ax1 = fig4.add_subplot(spec4[0, 0]) |
| 101 | +f4_ax1 = spec4[0, 0].add_subplot() |
103 | 102 | f4_ax1.annotate('GridSpec[0, 0]', **anno_opts)
|
104 |
| -fig4.add_subplot(spec4[0, 1]).annotate('GridSpec[0, 1:]', **anno_opts) |
105 |
| -fig4.add_subplot(spec4[1, 0]).annotate('GridSpec[1:, 0]', **anno_opts) |
106 |
| -fig4.add_subplot(spec4[1, 1]).annotate('GridSpec[1:, 1:]', **anno_opts) |
| 103 | +spec4[0, 1].add_subplot().annotate('GridSpec[0, 1:]', **anno_opts) |
| 104 | +spec4[1, 0].add_subplot().annotate('GridSpec[1:, 0]', **anno_opts) |
| 105 | +spec4[1, 1].add_subplot().annotate('GridSpec[1:, 1:]', **anno_opts) |
107 | 106 |
|
108 | 107 | ############################################################################
|
109 | 108 | # Another option is to use the ``width_ratios`` and ``height_ratios``
|
|
121 | 120 | height_ratios=heights)
|
122 | 121 | for row in range(3):
|
123 | 122 | for col in range(3):
|
124 |
| - ax = fig5.add_subplot(spec5[row, col]) |
| 123 | + ax = spec5[row, col].add_subplot() |
125 | 124 | label = 'Width: {}\nHeight: {}'.format(widths[col], heights[row])
|
126 | 125 | ax.annotate(label, (0.1, 0.5), xycoords='axes fraction', va='center')
|
127 | 126 |
|
|
154 | 153 | # remove the underlying axes
|
155 | 154 | for ax in f7_axs[1:, -1]:
|
156 | 155 | ax.remove()
|
157 |
| -axbig = fig7.add_subplot(gs[1:, -1]) |
| 156 | +axbig = gs[1:, -1].add_subplot() |
158 | 157 | axbig.annotate('Big Axes \nGridSpec[1:, -1]', (0.1, 0.5),
|
159 | 158 | xycoords='axes fraction', va='center')
|
160 | 159 |
|
|
172 | 171 |
|
173 | 172 | fig8 = plt.figure(constrained_layout=False)
|
174 | 173 | gs1 = fig8.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48, wspace=0.05)
|
175 |
| -f8_ax1 = fig8.add_subplot(gs1[:-1, :]) |
176 |
| -f8_ax2 = fig8.add_subplot(gs1[-1, :-1]) |
177 |
| -f8_ax3 = fig8.add_subplot(gs1[-1, -1]) |
| 174 | +f8_ax1 = gs1[:-1, :].add_subplot() |
| 175 | +f8_ax2 = gs1[-1, :-1].add_subplot() |
| 176 | +f8_ax3 = gs1[-1, -1].add_subplot() |
178 | 177 |
|
179 | 178 | ###############################################################################
|
180 | 179 | # This is similar to :func:`~matplotlib.pyplot.subplots_adjust`, but it only
|
|
185 | 184 | fig9 = plt.figure(constrained_layout=False)
|
186 | 185 | gs1 = fig9.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48,
|
187 | 186 | wspace=0.05)
|
188 |
| -f9_ax1 = fig9.add_subplot(gs1[:-1, :]) |
189 |
| -f9_ax2 = fig9.add_subplot(gs1[-1, :-1]) |
190 |
| -f9_ax3 = fig9.add_subplot(gs1[-1, -1]) |
| 187 | +f9_ax1 = gs1[:-1, :].add_subplot() |
| 188 | +f9_ax2 = gs1[-1, :-1].add_subplot() |
| 189 | +f9_ax3 = gs1[-1, -1].add_subplot() |
191 | 190 |
|
192 | 191 | gs2 = fig9.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98,
|
193 | 192 | hspace=0.05)
|
194 |
| -f9_ax4 = fig9.add_subplot(gs2[:, :-1]) |
195 |
| -f9_ax5 = fig9.add_subplot(gs2[:-1, -1]) |
196 |
| -f9_ax6 = fig9.add_subplot(gs2[-1, -1]) |
| 193 | +f9_ax4 = gs2[:, :-1].add_subplot() |
| 194 | +f9_ax5 = gs2[:-1, -1].add_subplot() |
| 195 | +f9_ax6 = gs2[-1, -1].add_subplot() |
197 | 196 |
|
198 | 197 | ###############################################################################
|
199 | 198 | # GridSpec using SubplotSpec
|
|
214 | 213 |
|
215 | 214 | for a in range(2):
|
216 | 215 | for b in range(3):
|
217 |
| - fig10.add_subplot(gs00[a, b]) |
218 |
| - fig10.add_subplot(gs01[b, a]) |
| 216 | + gs00[a, b].add_subplot() |
| 217 | + gs01[b, a].add_subplot() |
219 | 218 |
|
220 | 219 | ###############################################################################
|
221 | 220 | # A Complex Nested GridSpec using SubplotSpec
|
@@ -276,7 +275,7 @@ def squiggle_xy(a, b, c, d, i=np.arange(0.0, 2*np.pi, 0.05)):
|
276 | 275 |
|
277 | 276 | matplotlib.pyplot.subplots
|
278 | 277 | matplotlib.figure.Figure.add_gridspec
|
279 |
| -matplotlib.figure.Figure.add_subplot |
280 | 278 | matplotlib.gridspec.GridSpec
|
| 279 | +matplotlib.gridspec.SubplotSpec.add_subplot |
281 | 280 | matplotlib.gridspec.SubplotSpec.subgridspec
|
282 | 281 | matplotlib.gridspec.GridSpecFromSubplotSpec
|
0 commit comments