Skip to content

Backport PR #13469 on branch v3.0.2-doc (Add explanatory comments for text rotation example) #13472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ per-file-ignores =
examples/subplots_axes_and_figures/zoom_inset_axes.py: E402
examples/tests/backend_driver_sgskip.py: E402, E501
examples/text_labels_and_annotations/annotation_demo.py: E501
examples/text_labels_and_annotations/demo_text_rotation_mode.py: E402
examples/text_labels_and_annotations/custom_legends.py: E402
examples/text_labels_and_annotations/font_family_rc_sgskip.py: E402
examples/text_labels_and_annotations/font_file.py: E402
Expand Down
28 changes: 26 additions & 2 deletions examples/text_labels_and_annotations/demo_text_rotation_mode.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""
=======================
"""=======================
Demo Text Rotation Mode
=======================

The axes' method `~.axes.Axes.text` takes an argument ``rotation_mode`` that
controls the alignment and rotation of the text. If ``rotation_mode`` is
``None`` or ``default`` the text will first be rotated and then aligned
according to the horizontal and vertical alignments (``ha`` and ``va`` in the
example). If ``rotation_mode`` is ``anchor`` the text is aligned before
rotation.

"""
from mpl_toolkits.axes_grid1.axes_grid import ImageGrid

Expand All @@ -19,18 +25,23 @@ def test_rotation_mode(fig, mode, subplot_location):
for ha, ax in zip(ha_list, grid.axes_row[-1]):
ax.axis["bottom"].label.set_text(ha)

# create a grid of axes to display text on.
grid.axes_row[0][1].set_title(mode, size="large")

for va, ax in zip(va_list, grid.axes_column[0]):
ax.axis["left"].label.set_text(va)

# use a different horizontal and vertical alignment for the text in each
# axes.
i = 0
for va in va_list:
for ha in ha_list:
ax = grid[i]
for axis in ax.axis.values():
axis.toggle(ticks=False, ticklabels=False)

# add text to the axes. Set the rotation_mode, horizontal
# alignment (ha) and vertical alignment (va).
ax.text(0.5, 0.5, "Tpg",
size="large", rotation=40,
bbox=dict(boxstyle="square,pad=0.",
Expand All @@ -50,3 +61,16 @@ def test_rotation_mode(fig, mode, subplot_location):
test_rotation_mode(fig, "default", 121)
test_rotation_mode(fig, "anchor", 122)
plt.show()


#############################################################################
#
# ------------
#
# References
# """"""""""
#
# The use of the following method is shown in this example:

import matplotlib
matplotlib.axes.Axes.text