|
1 | 1 | """ |
2 | | -========= |
3 | | -Multiline |
4 | | -========= |
| 2 | +============== |
| 3 | +Multiline text |
| 4 | +============== |
5 | 5 |
|
| 6 | +Texts may contain newlines to create multiline text. |
| 7 | +
|
| 8 | +The text alignment of the individual lines can be controlled by the |
| 9 | +``multialignment`` parameter. If not set, the alignment of the lines is inferred from |
| 10 | +the ``horizontalalignment`` and ``verticalalignment`` parameters, which primarily |
| 11 | +control the alignment of the bounding box of the text to its anchor point; e.g. if the |
| 12 | +text has the anchor point to its right, all text lines will be aligned to the right |
| 13 | +so that they pick up that anchor point. |
| 14 | +
|
| 15 | +When using mutli-line labels, it is recommended to use a layout manager (e.g. |
| 16 | +``layout="constrained"``) to ensure that there is enough space for the labels.. |
6 | 17 | """ |
7 | 18 | import matplotlib.pyplot as plt |
8 | | -import numpy as np |
9 | 19 |
|
10 | | -fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(7, 4)) |
| 20 | +fig, ax = plt.subplots(layout="constrained") |
| 21 | + |
| 22 | +ax.bar(range(6), [0.3, 0.5, 0.8, 1.5, 2.5, 8], alpha=0.3) |
| 23 | + |
| 24 | +ax.set_ylabel('labels are centered,\nso is their multi-line text by default') |
| 25 | +ax.set_xlabel('xlabel with\nmultialignment="left"', multialignment="left") |
11 | 26 |
|
12 | | -ax0.set_aspect(1) |
13 | | -ax0.plot(np.arange(10)) |
14 | | -ax0.set_xlabel('this is an xlabel\n(with newlines!)') |
15 | | -ax0.set_ylabel('this is vertical\ntest', multialignment='center') |
16 | | -ax0.text(2, 7, 'this is\nyet another test', |
17 | | - rotation=45, |
18 | | - horizontalalignment='center', |
19 | | - verticalalignment='top', |
20 | | - multialignment='center') |
| 27 | +ax.text( |
| 28 | + 4.5, 6, |
| 29 | + 'these lines\n' |
| 30 | + 'are right aligned\n' |
| 31 | + 'because of horizontalalignment', |
| 32 | + horizontalalignment='right', |
| 33 | +) |
| 34 | + |
| 35 | +ax.text( |
| 36 | + 4.5, 4, |
| 37 | + 'but\n' |
| 38 | + 'we can override this\n' |
| 39 | + 'with an explicit multialingment="left"', |
| 40 | + horizontalalignment='right', |
| 41 | + multialignment='left', |
| 42 | +) |
| 43 | + |
| 44 | +plt.show() |
21 | 45 |
|
22 | | -ax0.grid() |
23 | 46 |
|
| 47 | +# %% |
| 48 | +# Each line has the same height irrespective of the used characters unless |
| 49 | +# there is math text involved, which may need more vertical space |
24 | 50 |
|
25 | | -ax1.text(0.29, 0.4, "Mat\nTTp\n123", size=18, |
26 | | - va="baseline", ha="right", multialignment="left", |
27 | | - bbox=dict(fc="none")) |
| 51 | +fig, ax = plt.subplots() |
28 | 52 |
|
29 | | -ax1.text(0.34, 0.4, "Mag\nTTT\n123", size=18, |
30 | | - va="baseline", ha="left", multialignment="left", |
31 | | - bbox=dict(fc="none")) |
| 53 | +ax.text(0.1, 0.4, "Mat\nTTp\n123", size=18, |
| 54 | + va="baseline", bbox=dict(fc="none", ec="tab:orange")) |
32 | 55 |
|
33 | | -ax1.text(0.95, 0.4, "Mag\nTTT$^{A^A}$\n123", size=18, |
34 | | - va="baseline", ha="right", multialignment="left", |
35 | | - bbox=dict(fc="none")) |
| 56 | +ax.text(0.3, 0.4, "Mag\nTTT\n123", size=18, |
| 57 | + va="baseline", bbox=dict(fc="none", ec="tab:orange")) |
36 | 58 |
|
37 | | -ax1.set_xticks([0.2, 0.4, 0.6, 0.8, 1.], |
38 | | - labels=["Jan\n2009", "Feb\n2009", "Mar\n2009", "Apr\n2009", |
39 | | - "May\n2009"]) |
| 59 | +ax.text(0.5, 0.4, "Mag\nTTT$^{A^A}$\n123", size=18, |
| 60 | + va="baseline", bbox=dict(fc="none", ec="tab:orange")) |
40 | 61 |
|
41 | | -ax1.axhline(0.4) |
42 | | -ax1.set_title("test line spacing for multiline text") |
| 62 | +ax.axhline(0.4) |
| 63 | +ax.set_title("test line spacing for multiline text") |
| 64 | +ax.set_xlim(0, 0.7) |
| 65 | +ax.yaxis.minorticks_on() |
| 66 | +ax.yaxis.set_tick_params(which="minor", grid_color="0.9") |
| 67 | +ax.grid(which="both") |
43 | 68 |
|
44 | | -fig.subplots_adjust(bottom=0.25, top=0.75) |
45 | 69 | plt.show() |
0 commit comments