10000 Document text alignment · matplotlib/matplotlib@0a1c39f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0a1c39f

Browse files
committed
Document text alignment
Closes #21571.
1 parent 1ff14f1 commit 0a1c39f

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

examples/text_labels_and_annotations/text_alignment.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,48 @@
11
"""
2-
===================
3-
Precise text layout
4-
===================
2+
==============
3+
Text alignment
4+
==============
5+
6+
Texts are aligned relative to their anchor point depending on the properties
7+
``horizontalalignment`` and ``verticalalignment``.
8+
9+
.. plot::
10+
11+
import matplotlib.pyplot as plt
12+
import numpy as np
13+
14+
y = [0.22, 0.34, 0.5, 0.56, 0.78]
15+
x = [0.17, 0.5, 0.855]
16+
X, Y = np.meshgrid(x, y)
17+
18+
fig, ax = plt.subplots(figsize=(6, 4), dpi=100)
19+
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[])
20+
ax.spines[:].set_visible(False)
21+
ax.text(0.5, 0.5, 'plot', fontsize=128, ha='center', va='center', zorder=1)
22+
ax.hlines(y, x[0], x[-1], color='grey')
23+
ax.vlines(x, y[0], y[-1], color='grey')
24+
ax.plot(X.ravel(), Y.ravel(), 'o')
25+
pad_x = 0.02
26+
pad_y = 0.04
27+
ax.text(x[0] - pad_x, y[0], 'bottom', ha='right', va='center')
28+
ax.text(x[0] - pad_x, y[1], 'baseline', ha='right', va='center')
29+
ax.text(x[0] - pad_x, y[2], 'center', ha='right', va='center')
30+
ax.text(x[0] - pad_x, y[3], 'center_baseline', ha='right', va='center')
31+
ax.text(x[0] - pad_x, y[4], 'top', ha='right', va='center')
32+
ax.text(x[0], y[0] - pad_y, 'left', ha='center', va='top')
33+
ax.text(x[1], y[0] - pad_y, 'center', ha='center', va='top')
34+
ax.text(x[2], y[0] - pad_y, 'right', ha='center', va='top')
35+
ax.set_xlabel('horizontalalignment', fontsize=14)
36+
ax.set_ylabel('verticalalignment', fontsize=14, labelpad=35)
37+
ax.set_title(
38+
'Relative position of text anchor point depending on alignment')
39+
plt.show()
540
6-
You can precisely layout text in data or axes coordinates. This example shows
7-
you some of the alignment and rotation specifications for text layout.
841
"""
942

43+
#############################################################################
44+
# The following plot uses this to align text relative to a plotted rectangle.
45+
1046
import matplotlib.pyplot as plt
1147

1248
fig, ax = plt.subplots()

lib/matplotlib/text.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ def __init__(self,
148148
"""
149149
Create a `.Text` instance at *x*, *y* with string *text*.
150150
151+
The text is aligned relative to the anchor point (*x*, *y*) according
152+
to ``horizontalalignment`` (default: 'left') and ``verticalalignment``
153+
(default: 'bottom'). See also
154+
:doc:`/gallery/text_labels_and_annotations/text_alignment`.
155+
151156
While Text accepts the 'label' keyword argument, by default it is not
152157
added to the handles of a legend.
153158
@@ -956,11 +961,13 @@ def set_color(self, color):
956961

957962
def set_horizontalalignment(self, align):
958963
"""
959-
Set the horizontal alignment to one of
964+
Set the horizontal alignment relative to the anchor point.
965+
966+
See also :doc:`/gallery/text_labels_and_annotations/text_alignment`.
960967
961968
Parameters
962969
----------
963-
align : {'center', 'right', 'left'}
970+
align : {'left', 'center', 'right'}
964971
"""
965972
_api.check_in_list(['center', 'right', 'left'], align=align)
966973
self._horizontalalignment = align
@@ -1202,11 +1209,13 @@ def set_transform_rotates_text(self, t):
12021209

12031210
def set_verticalalignment(self, align):
12041211
"""
1205-
Set the vertical alignment.
1212+
Set the vertical alignment relative to the anchor point.
1213+
1214+
See also :doc:`/gallery/text_labels_and_annotations/text_alignment`.
12061215
12071216
Parameters
12081217
----------
1209-
align : {'center', 'top', 'bottom', 'baseline', 'center_baseline'}
1218+
align : {'bottom', 'baseline', 'center', 'center_baseline', 'top'}
12101219
"""
12111220
_api.check_in_list(
12121221
['top', 'bottom', 'center', 'baseline', 'center_baseline'],

0 commit comments

Comments
 (0)
0