|
1 |
| -"""======================= |
| 1 | +""" |
| 2 | +======================= |
2 | 3 | Demo Text Rotation Mode
|
3 | 4 | =======================
|
4 | 5 |
|
5 |
| -The axes' method `~.axes.Axes.text` takes an argument ``rotation_mode`` that |
6 |
| -controls the alignment and rotation of the text. If ``rotation_mode`` is |
7 |
| -``None`` or ``default`` the text will first be rotated and then aligned |
8 |
| -according to the horizontal and vertical alignments (``ha`` and ``va`` in the |
9 |
| -example). If ``rotation_mode`` is ``anchor`` the text is aligned before |
10 |
| -rotation. |
| 6 | +This example illustrates the effect of ``rotation_mode`` on the positioning |
| 7 | +of rotated text. |
| 8 | +
|
| 9 | +Rotated `.Text`\s are created by passing the parameter ``rotation`` to |
| 10 | +the constructor or the axes' method `~.axes.Axes.text`. |
| 11 | +
|
| 12 | +The actual positioning depends on the additional parameters |
| 13 | +``horizontalalignment``, ``verticalalignment`` and ``rotation_mode``. |
| 14 | +``rotation_mode`` determines the order of rotation and alignment: |
| 15 | +
|
| 16 | +- ``roation_mode='default'`` (or None) first rotates the text and then aligns |
| 17 | + the bounding box of the rotated text. |
| 18 | +- ``roation_mode='anchor'`` aligns the unrotated text and then rotates the |
| 19 | + text around the point of alignment. |
11 | 20 |
|
12 | 21 | """
|
13 | 22 | from mpl_toolkits.axes_grid1.axes_grid import ImageGrid
|
14 | 23 |
|
15 | 24 |
|
16 | 25 | def test_rotation_mode(fig, mode, subplot_location):
|
17 |
| - ha_list = "left center right".split() |
18 |
| - va_list = "top center baseline bottom".split() |
| 26 | + ha_list = ["left", "center", "right"] |
| 27 | + va_list = ["top", "center", "baseline", "bottom"] |
19 | 28 | grid = ImageGrid(fig, subplot_location,
|
20 | 29 | nrows_ncols=(len(va_list), len(ha_list)),
|
21 | 30 | share_all=True, aspect=True,
|
22 |
| - # label_mode='1', |
| 31 | + axes_pad=0.1, |
23 | 32 | cbar_mode=None)
|
24 | 33 |
|
25 | 34 | for ha, ax in zip(ha_list, grid.axes_row[-1]):
|
26 | 35 | ax.axis["bottom"].label.set_text(ha)
|
27 | 36 |
|
28 |
| - # create a grid of axes to display text on. |
29 |
| - grid.axes_row[0][1].set_title(mode, size="large") |
30 |
| - |
31 | 37 | for va, ax in zip(va_list, grid.axes_column[0]):
|
32 | 38 | ax.axis["left"].label.set_text(va)
|
33 | 39 |
|
| 40 | + grid.axes_row[0][1].set_title(f"rotation_mode='{mode}'", size="x-large") |
| 41 | + |
34 | 42 | # use a different horizontal and vertical alignment for the text in each
|
35 | 43 | # axes.
|
36 | 44 | i = 0
|
37 | 45 | for va in va_list:
|
38 | 46 | for ha in ha_list:
|
39 | 47 | ax = grid[i]
|
| 48 | + ax.set_frame_on(False) |
40 | 49 | for axis in ax.axis.values():
|
41 | 50 | axis.toggle(ticks=False, ticklabels=False)
|
| 51 | + ax.axvline(0.5, color='0.7', zorder=1) |
| 52 | + ax.axhline(0.5, color='0.7', zorder=1) |
| 53 | + ax.plot(0.5, 0.5, 'o', color='tab:orange') |
42 | 54 |
|
43 | 55 | # add text to the axes. Set the rotation_mode, horizontal
|
44 | 56 | # alignment (ha) and vertical alignment (va).
|
45 | 57 | ax.text(0.5, 0.5, "Tpg",
|
46 |
| - size="large", rotation=40, |
| 58 | + size=18, rotation=40, |
47 | 59 | bbox=dict(boxstyle="square,pad=0.",
|
48 |
| - ec="none", fc="0.5", alpha=0.5), |
| 60 | + ec="none", fc='tab:blue', alpha=0.5), |
49 | 61 | ha=ha, va=va,
|
50 | 62 | rotation_mode=mode)
|
51 |
| - ax.axvline(0.5) |
52 |
| - ax.axhline(0.5) |
53 | 63 | i += 1
|
54 | 64 |
|
55 | 65 |
|
56 | 66 | if __name__ == "__main__":
|
57 | 67 | import matplotlib.pyplot as plt
|
58 |
| - fig = plt.figure(figsize=(5.5, 4)) |
| 68 | + fig = plt.figure(figsize=(8, 6)) |
59 | 69 | test_rotation_mode(fig, "default", 121)
|
60 | 70 | test_rotation_mode(fig, "anchor", 122)
|
61 | 71 | plt.show()
|
|
0 commit comments