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