|
1 |
| -"""======================= |
| 1 | +r""" |
| 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 | """
|
| 22 | +import matplotlib.pyplot as plt |
13 | 23 | from mpl_toolkits.axes_grid1.axes_grid import ImageGrid
|
14 | 24 |
|
15 | 25 |
|
16 | 26 | def test_rotation_mode(fig, mode, subplot_location):
|
17 |
| - ha_list = "left center right".split() |
18 |
| - va_list = "top center baseline bottom".split() |
| 27 | + ha_list = ["left", "center", "right"] |
| 28 | + va_list = ["top", "center", "baseline", "bottom"] |
19 | 29 | grid = ImageGrid(fig, subplot_location,
|
20 | 30 | nrows_ncols=(len(va_list), len(ha_list)),
|
21 |
| - share_all=True, aspect=True, |
22 |
| - # label_mode='1', |
23 |
| - cbar_mode=None) |
| 31 | + share_all=True, aspect=True, cbar_mode=None) |
24 | 32 |
|
| 33 | + # labels and title |
25 | 34 | for ha, ax in zip(ha_list, grid.axes_row[-1]):
|
26 | 35 | ax.axis["bottom"].label.set_text(ha)
|
27 |
| - |
28 |
| - # create a grid of axes to display text on. |
29 |
| - grid.axes_row[0][1].set_title(mode, size="large") |
30 |
| - |
31 | 36 | for va, ax in zip(va_list, grid.axes_column[0]):
|
32 | 37 | ax.axis["left"].label.set_text(va)
|
| 38 | + grid.axes_row[0][1].set_title(f"rotation_mode='{mode}'", size="large") |
| 39 | + |
| 40 | + if mode == "default": |
| 41 | + kw = dict() |
| 42 | + else: |
| 43 | + kw = dict( |
| 44 | + bbox=dict(boxstyle="square,pad=0.", ec="none", fc="C1", alpha=0.3)) |
| 45 | + |
| 46 | + # use a different text alignment in each axes |
| 47 | + texts = [] |
| 48 | + for (va, ha), ax in zip([(x, y) for x in va_list for y in ha_list], grid): |
| 49 | + # prepare axes layout |
| 50 | + for axis in ax.axis.values(): |
| 51 | + axis.toggle(ticks=False, ticklabels=False) |
| 52 | + ax.axis([0, 1, 0, 1]) |
| 53 | + ax.axvline(0.5, color="skyblue", zorder=0) |
| 54 | + ax.axhline(0.5, color="skyblue", zorder=0) |
| 55 | + ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1) |
| 56 | + |
| 57 | + # add text with rotation and alignment settings |
| 58 | + tx = ax.text(0.5, 0.5, "Tpg", |
| 59 | + size="x-large", rotation=40, |
| 60 | + horizontalalignment=ha, verticalalignment=va, |
| 61 | + rotation_mode=mode, **kw) |
| 62 | + texts.append(tx) |
| 63 | + |
| 64 | + if mode == "default": |
| 65 | + # highlight bbox |
| 66 | + fig.canvas.draw() |
| 67 | + for ax, tx in zip(grid, texts): |
| 68 | + bb = tx.get_window_extent().inverse_transformed(ax.transData) |
| 69 | + rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height, |
| 70 | + facecolor="C1", alpha=0.3, zorder=2) |
| 71 | + ax.add_patch(rect) |
| 72 | + |
33 | 73 |
|
34 |
| - # use a different horizontal and vertical alignment for the text in each |
35 |
| - # axes. |
36 |
| - i = 0 |
37 |
| - for va in va_list: |
38 |
| - for ha in ha_list: |
39 |
| - ax = grid[i] |
40 |
| - for axis in ax.axis.values(): |
41 |
| - axis.toggle(ticks=False, ticklabels=False) |
42 |
| - |
43 |
| - # add text to the axes. Set the rotation_mode, horizontal |
44 |
| - # alignment (ha) and vertical alignment (va). |
45 |
| - ax.text(0.5, 0.5, "Tpg", |
46 |
| - size="large", rotation=40, |
47 |
| - bbox=dict(boxstyle="square,pad=0.", |
48 |
| - ec="none", fc="0.5", alpha=0.5), |
49 |
| - ha=ha, va=va, |
50 |
| - rotation_mode=mode) |
51 |
| - ax.axvline(0.5) |
52 |
| - ax.axhline(0.5) |
53 |
| - i += 1 |
54 |
| - |
55 |
| - |
56 |
| -if __name__ == "__main__": |
57 |
| - import matplotlib.pyplot as plt |
58 |
| - fig = plt.figure(figsize=(5.5, 4)) |
59 |
| - test_rotation_mode(fig, "default", 121) |
60 |
| - test_rotation_mode(fig, "anchor", 122) |
61 |
| - plt.show() |
| 74 | +fig = plt.figure(figsize=(8, 6)) |
| 75 | +test_rotation_mode(fig, "default", 121) |
| 76 | +test_rotation_mode(fig, "anchor", 122) |
| 77 | +plt.show() |
62 | 78 |
|
63 | 79 |
|
64 | 80 | #############################################################################
|
|
0 commit comments