|
1 | 1 | """
|
2 |
| -=========== |
3 |
| -Titles Demo |
4 |
| -=========== |
| 2 | +================= |
| 3 | +Title positioning |
| 4 | +================= |
5 | 5 |
|
6 |
| -matplotlib can display plot titles centered, flush with the left side of |
| 6 | +Matplotlib can display plot titles centered, flush with the left side of |
7 | 7 | a set of axes, and flush with the right side of a set of axes.
|
8 | 8 |
|
9 | 9 | """
|
|
16 | 16 | plt.title('Right Title', loc='right')
|
17 | 17 |
|
18 | 18 | plt.show()
|
| 19 | + |
| 20 | +########################################################################### |
| 21 | +# The vertical position is automatically chosen to avoid decorators on the |
| 22 | +# topmost x-axis: |
| 23 | + |
| 24 | +fig, axs = plt.subplots(1, 2, constrained_layout=True) |
| 25 | + |
| 26 | +ax = axs[0] |
| 27 | +ax.plot(range(10)) |
| 28 | +ax.xaxis.set_label_position('top') |
| 29 | +ax.set_xlabel('X-label') |
| 30 | +ax.set_title('Center Title') |
| 31 | + |
| 32 | +ax = axs[1] |
| 33 | +ax.plot(range(10)) |
| 34 | +ax.xaxis.set_label_position('top') |
| 35 | +ax.xaxis.tick_top() |
| 36 | +ax.set_xlabel('X-label') |
| 37 | +ax.set_title('Center Title') |
| 38 | +plt.show() |
| 39 | + |
| 40 | +########################################################################### |
| 41 | +# Automatic positioning can be turned off by manually specifying the |
| 42 | +# *y* kwarg for the title or setting :rc:`axes.titley` in the rcParams. |
| 43 | + |
| 44 | +fig, axs = plt.subplots(1, 2, constrained_layout=True) |
| 45 | + |
| 46 | +ax = axs[0] |
| 47 | +ax.plot(range(10)) |
| 48 | +ax.xaxis.set_label_position('top') |
| 49 | +ax.set_xlabel('X-label') |
| 50 | +ax.set_title('Manual y', y=1.0, pad=-14) |
| 51 | + |
| 52 | +plt.rcParams['axes.titley'] = 1.0 # y is in axes-relative co-ordinates. |
| 53 | +plt.rcParams['axes.titlepad'] = -14 # pad is in points... |
| 54 | +ax = axs[1] |
| 55 | +ax.plot(range(10)) |
| 56 | +ax.set_xlabel('X-label') |
| 57 | +ax.set_title('rcParam y') |
| 58 | + |
| 59 | +plt.show() |
0 commit comments