Skip to content

Commit 5a583a4

Browse files
committed
Improve Demo Text Rotation Mode
1 parent 1e91f11 commit 5a583a4

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

examples/text_labels_and_annotations/demo_text_rotation_mode.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,71 @@
1-
"""=======================
1+
"""
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
"""
1322
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid
1423

1524

1625
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"]
1928
grid = ImageGrid(fig, subplot_location,
2029
nrows_ncols=(len(va_list), len(ha_list)),
2130
share_all=True, aspect=True,
22-
# label_mode='1',
31+
axes_pad=0.1,
2332
cbar_mode=None)
2433

2534
for ha, ax in zip(ha_list, grid.axes_row[-1]):
2635
ax.axis["bottom"].label.set_text(ha)
2736

28-
# create a grid of axes to display text on.
29-
grid.axes_row[0][1].set_title(mode, size="large")
30-
3137
for va, ax in zip(va_list, grid.axes_column[0]):
3238
ax.axis["left"].label.set_text(va)
3339

40+
grid.axes_row[0][1].set_title(f"rotation_mode='{mode}'", size="x-large")
41+
3442
# use a different horizontal and vertical alignment for the text in each
3543
# axes.
3644
i = 0
3745
for va in va_list:
3846
for ha in ha_list:
3947
ax = grid[i]
48+
ax.set_frame_on(False)
4049
for axis in ax.axis.values():
4150
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')
4254

4355
# add text to the axes. Set the rotation_mode, horizontal
4456
# alignment (ha) and vertical alignment (va).
4557
ax.text(0.5, 0.5, "Tpg",
46-
size="large", rotation=40,
58+
size=18, rotation=40,
4759
bbox=dict(boxstyle="square,pad=0.",
48-
ec="none", fc="0.5", alpha=0.5),
60+
ec="none", fc='tab:blue', alpha=0.5),
4961
ha=ha, va=va,
5062
rotation_mode=mode)
51-
ax.axvline(0.5)
52-
ax.axhline(0.5)
5363
i += 1
5464

5565

5666
if __name__ == "__main__":
5767
import matplotlib.pyplot as plt
58-
fig = plt.figure(figsize=(5.5, 4))
68+
fig = plt.figure(figsize=(8, 6))
5969
test_rotation_mode(fig, "default", 121)
6070
test_rotation_mode(fig, "anchor", 122)
6171
plt.show()

0 commit comments

Comments
 (0)