|
1 | 1 | """ |
2 | | -=============== |
3 | | -Aligning Labels |
4 | | -=============== |
| 2 | +========================== |
| 3 | +Aligning Labels and Titles |
| 4 | +========================== |
5 | 5 |
|
6 | | -Aligning xlabel and ylabel using `.Figure.align_xlabels` and |
7 | | -`.Figure.align_ylabels` |
| 6 | +Aligning xlabel, ylabel, and title using `.Figure.align_xlabels`, |
| 7 | +`.Figure.align_ylabels`, and `.Figure.align_titles`. |
8 | 8 |
|
9 | | -`.Figure.align_labels` wraps these two functions. |
| 9 | +`.Figure.align_labels` wraps the x and y label functions. |
10 | 10 |
|
11 | 11 | Note that the xlabel "XLabel1 1" would normally be much closer to the |
12 | | -x-axis, and "YLabel1 0" would be much closer to the y-axis of their |
13 | | -respective axes. |
| 12 | +x-axis, "YLabel0 0" would be much closer to the y-axis, and title |
| 13 | +"Title0 0" would be much closer to the top of their respective axes. |
14 | 14 | """ |
15 | 15 | import matplotlib.pyplot as plt |
16 | 16 | import numpy as np |
17 | 17 |
|
18 | | -import matplotlib.gridspec as gridspec |
| 18 | +fig, axs = plt.subplots(2, 2, layout='tight') |
19 | 19 |
|
20 | | -fig = plt.figure(tight_layout=True) |
21 | | -gs = gridspec.GridSpec(2, 2) |
22 | | - |
23 | | -ax = fig.add_subplot(gs[0, :]) |
| 20 | +ax = axs[0][0] |
24 | 21 | ax.plot(np.arange(0, 1e6, 1000)) |
25 | | -ax.set_ylabel('YLabel0') |
26 | | -ax.set_xlabel('XLabel0') |
| 22 | +ax.set_title('Title0 0') |
| 23 | +ax.set_ylabel('YLabel0 0') |
| 24 | + |
| 25 | +ax = axs[0][1] |
| 26 | +ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1)) |
| 27 | +ax.set_title('Title0 1') |
| 28 | +ax.xaxis.tick_top() |
| 29 | +ax.tick_params(axis='x', rotation=55) |
| 30 | + |
27 | 31 |
|
28 | 32 | for i in range(2): |
29 | | - ax = fig.add_subplot(gs[1, i]) |
| 33 | + ax = axs[1][i] |
30 | 34 | ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1)) |
31 | 35 | ax.set_ylabel('YLabel1 %d' % i) |
32 | 36 | ax.set_xlabel('XLabel1 %d' % i) |
33 | 37 | if i == 0: |
34 | 38 | ax.tick_params(axis='x', rotation=55) |
| 39 | + |
35 | 40 | fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels() |
| 41 | +fig.align_titles() |
36 | 42 |
|
37 | 43 | plt.show() |
0 commit comments