11"""
2+ .. redirect-from:: /tutorials/intermediate/gridspec
3+
4+ .. _arranging_axes:
5+
26===================================
37Arranging multiple Axes in a Figure
48===================================
5862`~matplotlib.gridspec.SubplotSpec`
5963 Specifies the location of the subplot in the given `.GridSpec`.
6064
65+ .. _fixed_size_axes:
66+
6167Adding single Axes at a time
6268----------------------------
6369
8086 Similar to `.pyplot.subplot`, but uses 0-based indexing and two-d python
8187 slicing to choose cells.
8288
83- .. redirect-from:: /tutorials/intermediate/gridspec
84-
8589"""
90+
91+ # %%
92+ #
93+ # As a simple example of manually adding an axes a, lets add a 3 inch x 2 inch
94+ # Axes to a 4 inch x 3 inch figure. Note that the location of the subplot is
95+ # defined as [left, bottom, width, height] in figure-normalized units:
96+
97+ # sphinx_gallery_thumbnail_number = 2
98+
99+ import matplotlib .pyplot as plt
100+ import numpy as np
101+
102+ w , h = 4 , 3
103+ margin = 0.5
104+ fig = plt .figure (figsize = (w , h ), facecolor = 'lightblue' )
105+ ax = fig .add_axes ([margin / w , margin / h , (w - 2 * margin ) / w ,
106+ (h - 2 * margin ) / h ])
107+
108+
86109# %%
87110# High-level methods for making grids
88111# ===================================
97120# we use `~.Axes.annotate`, but other examples could be `~.Axes.plot`,
98121# `~.Axes.pcolormesh`, etc.
99122
100- import matplotlib .pyplot as plt
101- import numpy as np
102-
103123fig , axs = plt .subplots (ncols = 2 , nrows = 2 , figsize = (5.5 , 3.5 ),
104124 layout = "constrained" )
105125# add an artist, in this case a nice label in the middle...
@@ -146,7 +166,8 @@ def annotate_axes(ax, text, fontsize=18):
146166# have a set aspect ratio. This leads to large gaps between Axes by default:
147167#
148168
149- fig , axs = plt .subplots (2 , 2 , layout = "constrained" , figsize = (5.5 , 3.5 ))
169+ fig , axs = plt .subplots (2 , 2 , layout = "constrained" ,
170+ figsize = (5.5 , 3.5 ), facecolor = 'lightblue' )
150171for ax in axs .flat :
151172 ax .set_aspect (1 )
152173fig .suptitle ('Fixed aspect Axes' )
@@ -159,7 +180,8 @@ def annotate_axes(ax, text, fontsize=18):
159180# provides `~.mpl_toolkits.axes_grid1.axes_grid.ImageGrid` to accomplish
160181# a similar effect, but with a non-standard Axes class).
161182
162- fig , axs = plt .subplots (2 , 2 , layout = "compressed" , figsize = (5.5 , 3.5 ))
183+ fig , axs = plt .subplots (2 , 2 , layout = "compressed" , figsize = (5.5 , 3.5 ),
184+ facecolor = 'lightblue' )
163185for ax in axs .flat :
164186 ax .set_aspect (1 )
165187fig .suptitle ('Fixed aspect Axes: compressed' )
@@ -218,7 +240,7 @@ def annotate_axes(ax, text, fontsize=18):
218240fig = plt .figure (layout = "constrained" )
219241subfigs = fig .subfigures (1 , 2 , wspace = 0.07 , width_ratios = [1.5 , 1. ])
220242axs0 = subfigs [0 ].subplots (2 , 2 )
221- subfigs [0 ].set_facecolor ('0.9 ' )
243+ subfigs [0 ].set_facecolor ('lightblue ' )
222244subfigs [0 ].suptitle ('subfigs[0]\n Left side' )
223245subfigs [0 ].supxlabel ('xlabel for subfigs[0]' )
224246
@@ -315,7 +337,7 @@ def annotate_axes(ax, text, fontsize=18):
315337# These spacing parameters can also be passed to `~.pyplot.subplots` and
316338# `~.pyplot.subplot_mosaic` as the *gridspec_kw* argument.
317339
318- fig = plt .figure (layout = None , facecolor = '0.9 ' )
340+ fig = plt .figure (layout = None , facecolor = 'lightblue ' )
319341gs = fig .add_gridspec (nrows = 3 , ncols = 3 , left = 0.05 , right = 0.75 ,
320342 hspace = 0.1 , wspace = 0.05 )
321343ax0 = fig .add_subplot (gs [:- 1 , :])
0 commit comments