Skip to content

Commit 20f9c27

Browse files
authored
Merge pull request #13644 from meeseeksmachine/auto-backport-of-pr-13612-on-v3.1.x
Backport PR #13612 on branch v3.1.x (Improve Demo Text Rotation Mode)
2 parents e8f6813 + 02d0630 commit 20f9c27

File tree

1 file changed

+60
-44
lines changed

1 file changed

+60
-44
lines changed

examples/text_labels_and_annotations/demo_text_rotation_mode.py

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,80 @@
1-
"""=======================
1+
r"""
2+
=======================
23
Demo Text Rotation Mode
34
=======================
45
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.
1120
1221
"""
22+
import matplotlib.pyplot as plt
1323
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid
1424

1525

1626
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"]
1929
grid = ImageGrid(fig, subplot_location,
2030
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)
2432

33+
# labels and title
2534
for ha, ax in zip(ha_list, grid.axes_row[-1]):
2635
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-
3136
for va, ax in zip(va_list, grid.axes_column[0]):
3237
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+
3373

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()
6278

6379

6480
#############################################################################

0 commit comments

Comments
 (0)