1"""
2=============
3GridSpec demo
4=============
5
6This example demonstrates the use of `.GridSpec` to generate subplots,
7the control of the relative sizes of subplots with *width_ratios* and
8*height_ratios*, and the control of the spacing around and between subplots
9using subplot params (*left*, *right*, *bottom*, *top*, *wspace*, and
10*hspace*).
11"""
12
13import matplotlib.pyplot as plt
14from matplotlib.gridspec import GridSpec
15
16
17def annotate_axes(fig):
18    for i, ax in enumerate(fig.axes):
19        ax.text(0.5, 0.5, "ax%d" % (i+1), va="center", ha="center")
20        ax.tick_params(labelbottom=False, labelleft=False)
21
22
23fig = plt.figure()
24fig.suptitle("Controlling subplot sizes with width_ratios and height_ratios")
25
26gs = GridSpec(2, 2, width_ratios=[1, 2], height_ratios=[4, 1])
27ax1 = fig.add_subplot(gs[0])
28ax2 = fig.add_subplot(gs[1])
29ax3 = fig.add_subplot(gs[2])
30ax4 = fig.add_subplot(gs[3])
31
32annotate_axes(fig)
33
34
35fig = plt.figure()
36fig.suptitle("Controlling spacing around and between subplots")
37
38gs1 = GridSpec(3, 3, left=0.05, right=0.48, wspace=0.05)
39ax1 = fig.add_subplot(gs1[:-1, :])
40ax2 = fig.add_subplot(gs1[-1, :-1])
41ax3 = fig.add_subplot(gs1[-1, -1])
42
43gs2 = GridSpec(3, 3, left=0.55, right=0.98, hspace=0.05)
44ax4 = fig.add_subplot(gs2[:, :-1])
45ax5 = fig.add_subplot(gs2[:-1, -1])
46ax6 = fig.add_subplot(gs2[-1, -1])
47
48annotate_axes(fig)
49
50plt.show()
51