|
8 | 8 |
|
9 | 9 | `.Figure.align_labels` wraps the x and y label functions.
|
10 | 10 |
|
11 |
| -Note that the xlabel "XLabel1 1" would normally be much closer to the |
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. |
| 11 | +We align the xlabels and ylabels using short calls to `.Figure.align_xlabels` |
| 12 | +and `.Figure.align_ylabels`. We also show a manual way to align the ylabels |
| 13 | +using the `~.Axis.set_label_coords` method of the yaxis object. Note this requires |
| 14 | +knowing a good offset value which is hardcoded. |
| 15 | +
|
| 16 | +.. redirect-from:: /gallery/pyplots/align_ylabels |
14 | 17 | """
|
| 18 | + |
15 | 19 | import matplotlib.pyplot as plt
|
16 | 20 | import numpy as np
|
17 | 21 |
|
18 |
| -fig, axs = plt.subplots(2, 2, layout='constrained') |
19 |
| - |
20 |
| -ax = axs[0][0] |
21 |
| -ax.plot(np.arange(0, 1e6, 1000)) |
22 |
| -ax.set_title('Title0 0') |
23 |
| -ax.set_ylabel('YLabel0 0') |
| 22 | +fig, axs = plt.subplots(2, 3, figsize=(8.9, 5.5), |
| 23 | + layout='constrained', gridspec_kw={'wspace': 0.1}) |
24 | 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.set_xticks(ax.get_xticks()) |
30 |
| -ax.tick_params(axis='x', rotation=55, rotation_mode='xtick') |
| 25 | +# add sample data and labels |
| 26 | +for ax in axs.flat: |
| 27 | + scale = 2000 if ax.get_subplotspec().is_first_row() else 1 |
| 28 | + ax.plot(scale * (1 - np.exp(-np.linspace(0, 5, 100)))) |
| 29 | + if ax.get_subplotspec().is_last_row(): |
| 30 | + ax.set_xlabel('xlabel', bbox=dict(facecolor='yellow', pad=5, alpha=0.2)) |
| 31 | + ax.set_ylabel('ylabel', bbox=dict(facecolor='yellow', pad=5, alpha=0.2)) |
| 32 | + ax.set_ylim(0, scale) |
31 | 33 |
|
| 34 | +# Modify ticks to get different margins in some plots |
| 35 | +axs[0, 0].xaxis.tick_top() |
| 36 | +axs[1, 2].tick_params(axis='x', rotation=55) |
| 37 | +axs[0, 0].set_title('ylabels not aligned') |
32 | 38 |
|
33 |
| -for i in range(2): |
34 |
| - ax = axs[1][i] |
35 |
| - ax.plot(np.arange(1., 0., -0.1) * 2000., np.arange(1., 0., -0.1)) |
36 |
| - ax.set_ylabel('YLabel1 %d' % i) |
37 |
| - ax.set_xlabel('XLabel1 %d' % i) |
38 |
| - if i == 0: |
39 |
| - ax.set_xticks(ax.get_xticks()) |
40 |
| - ax.tick_params(axis='x', rotation=55, rotation_mode='xtick') |
| 39 | +# Align labels |
| 40 | +fig.align_titles() # Align titles |
| 41 | +fig.align_xlabels() # Align all x-axis labels |
| 42 | +fig.align_ylabels(axs[:, 1]) # Align only the second column's y-labels |
| 43 | +axs[0, 1].set_title('fig.align_ylabels()') |
41 | 44 |
|
42 |
| -fig.align_labels() # same as fig.align_xlabels(); fig.align_ylabels() |
43 |
| -fig.align_titles() |
| 45 | +# Manually adjust y-labels for the third column |
| 46 | +for ax in axs[:, 2]: |
| 47 | + ax.yaxis.set_label_coords(-0.3, 0.5) |
| 48 | +axs[0, 2].set_title('ylabels manually aligned') |
44 | 49 |
|
45 | 50 | plt.show()
|
46 | 51 |
|
| 52 | + |
| 53 | +# %% |
| 54 | +# |
| 55 | +# .. admonition:: References |
| 56 | +# |
| 57 | +# The use of the following functions, methods, classes and modules is shown |
| 58 | +# in this example: |
| 59 | +# |
| 60 | +# - `matplotlib.figure.Figure.align_xlabels` |
| 61 | +# - `matplotlib.figure.Figure.align_ylabels` |
| 62 | +# - `matplotlib.figure.Figure.align_labels` |
| 63 | +# - `matplotlib.figure.Figure.align_titles` |
| 64 | +# - `matplotlib.axis.Axis.set_label_coords` |
| 65 | +# - `matplotlib.axes.Axes.plot` / `matplotlib.pyplot.plot` |
| 66 | +# - `matplotlib.axes.Axes.set_title` |
| 67 | +# - `matplotlib.axes.Axes.set_ylabel` |
| 68 | +# - `matplotlib.axes.Axes.set_ylim` |
| 69 | + |
47 | 70 | # %%
|
48 | 71 | # .. tags::
|
49 | 72 | #
|
|
0 commit comments