8000 Document text alignment · matplotlib/matplotlib@7892681 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7892681

Browse files
committed
Document text alignment
Closes #21571.
1 parent e974bd9 commit 7892681

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

examples/text_labels_and_annotations/text_alignment.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
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.77]
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=40)
37+
ax.set_title('Relative position of text anchor point depending on alignment')
38+
plt.show()
539
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.
840
"""
941

42+
#############################################################################
43+
# The following plot uses this to align text relative to a plotted rectangle.
44+
1045
import matplotlib.pyplot as plt
1146

1247
fig, ax = plt.subplots()

lib/matplotlib/text.py

Lines changed: 13 additions & 4 deletions
< D276 /tr>
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ def __init__(self,
132132
"""
133133
Create a `.Text` instance at *x*, *y* with string *text*.
134134
135+
The text is aligned relative to the anchor point (*x*, *y*) according
136+
to ``horizontalalingment`` (default: 'left') and ``verticalalignment``
137+
(default: 'bottom'). See also
138+
:doc:`/gallery/text_labels_and_annotations/text_alignment`.
139+
135140
Valid keyword arguments are:
136141
137142
%(Text:kwdoc)s
@@ -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
@@ -1194,11 +1201,13 @@ def set_transform_rotates_text(self, t):
11941201

11951202
def set_verticalalignment(self, align):
11961203
"""
1197-
Set the vertical alignment.
1204+
Set the vertical alignment relative to the anchor point.
1205+
1206+
See also :doc:`/gallery/text_labels_and_annotations/text_alignment`.
11981207
11991208
Parameters
12001209
----------
1201-
align : {'center', 'top', 'bottom', 'baseline', 'center_baseline'}
1210+
align : {'bottom', 'baseline', 'center', 'center_baseline', 'top'}
12021211
"""
12031212
_api.check_in_list(
12041213
['top', 'bottom', 'center', 'baseline', 'center_baseline'],

0 commit comments

Comments
 (0)
0