1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
r"""
==================
Text rotation mode
==================
This example illustrates the effect of ``rotation_mode`` on the positioning
of rotated text.
Rotated `.Text`\s are created by passing the parameter ``rotation`` to
the constructor or the Axes' method `~.axes.Axes.text`.
The actual positioning depends on the additional parameters
``horizontalalignment``, ``verticalalignment`` and ``rotation_mode``.
``rotation_mode`` determines the order of rotation and alignment:
- ``rotation_mode='default'`` (or None) first rotates the text and then aligns
the bounding box of the rotated text.
- ``rotation_mode='anchor'`` aligns the unrotated text and then rotates the
text around the point of alignment.
.. redirect-from:: /gallery/text_labels_and_annotations/text_rotation
"""
import matplotlib.pyplot as plt
def test_rotation_mode(fig, mode):
ha_list = ["left", "center", "right"]
va_list = ["top", "center", "baseline", "bottom"]
axs = fig.subplots(len(va_list), len(ha_list), sharex=True, sharey=True,
subplot_kw=dict(aspect=1),
gridspec_kw=dict(hspace=0, wspace=0))
# labels and title
for ha, ax in zip(ha_list, axs[-1, :]):
ax.set_xlabel(ha)
for va, ax in zip(va_list, axs[:, 0]):
ax.set_ylabel(va)
axs[0, 1].set_title(f"rotation_mode='{mode}'", size="large")
kw = (
{} if mode == "default" else
{"bbox": dict(boxstyle="square,pad=0.", ec="none", fc="C1", alpha=0.3)}
)
texts = {}
# use a different text alignment in each Axes
for i, va in enumerate(va_list):
for j, ha in enumerate(ha_list):
ax = axs[i, j]
# prepare Axes layout
ax.set(xticks=[], yticks=[])
ax.axvline(0.5, color="skyblue", zorder=0)
ax.axhline(0.5, color="skyblue", zorder=0)
ax.plot(0.5, 0.5, color="C0", marker="o", zorder=1)
# add text with rotation and alignment settings
tx = ax.text(0.5, 0.5, "Tpg",
size="x-large", rotation=40,
horizontalalignment=ha, verticalalignment=va,
rotation_mode=mode, **kw)
texts[ax] = tx
if mode == "default":
# highlight bbox
fig.canvas.draw()
for ax, text in texts.items():
bb = text.get_window_extent().transformed(ax.transData.inverted())
rect = plt.Rectangle((bb.x0, bb.y0), bb.width, bb.height,
facecolor="C1", alpha=0.3, zorder=2)
ax.add_patch(rect)
fig = plt.figure(figsize=(8, 5))
subfigs = fig.subfigures(1, 2)
test_rotation_mode(subfigs[0], "default")
test_rotation_mode(subfigs[1], "anchor")
plt.show()
# %%
#
# .. admonition:: References
#
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.axes.Axes.text` / `matplotlib.pyplot.text`
|