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

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6859d9d

Browse files
committed
Document text alignment
Closes #21571.
1 parent c12791c commit 6859d9d

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

examples/text_labels_and_annotations/text_alignment.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
11
"""
2-
===================
3-
Precise text layout
4-
===================
2+
==============
3+
Text alignment
4+
==============
55
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.
6+
Texts are aligned relative to their anchor point depending on the properties
7+
``horizontalalignment`` and ``verticalalignment``.
88
"""
99

1010
import matplotlib.pyplot as plt
11+
import numpy as np
12+
13+
y = [0.21, 0.33, 0.5, 0.558, 0.785]
14+
x = [0.17, 0.5, 0.855]
15+
X, Y = np.meshgrid(x, y)
16+
17+
fig, ax = plt.subplots(dpi=100)
18+
ax.set(xlim=(0, 1), ylim=(0, 1), xticks=[], yticks=[])
19+
ax.spines[:].set_visible(False)
20+
ax.text(0.5, 0.5, 'plot', fontsize=128, ha='center', va='center', zorder=1)
21+
ax.hlines(y, x[0], x[-1], color='grey')
22+
ax.vlines(x, y[0], y[-1], color='grey')
23+
ax.plot(X.ravel(), Y.ravel(), 'o')
24+
pad_x = 0.02
25+
pad_y = 0.04
26+
ax.text(x[0] - pad_x, y[0], 'bottom', ha='right', va='center')
27+
ax.text(x[0] - pad_x, y[1], 'baseline', ha='right', va='center')
28+
ax.text(x[0] - pad_x, y[2], 'center', ha='right', va='center')
29+
ax.text(x[0] - pad_x, y[3], 'center_baseline', ha='right', va='center')
30+
ax.text(x[0] - pad_x, y[4], 'top', ha='right', va='center')
31+
ax.text(x[0], y[0] - pad_y, 'left', ha='center', va='top')
32+
ax.text(x[1], y[0] - pad_y, 'center', ha='center', va='top')
33+
ax.text(x[2], y[0] - pad_y, 'right', ha='center', va='top')
34+
ax.set_xlabel('horizontalalignment')
35+
ax.set_ylabel('verticalalignment', labelpad=45)
36+
ax.set_title('Relative position of text anchor point depending on alignment')
37+
plt.show()
38+
39+
40+
#############################################################################
41+
# The following plot uses this to align text relative to a plotted rectangle.
1142

1243
fig, ax = plt.subplots()
1344

lib/matplotlib/text.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ 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 :doc:`/gallery/text/text_alignment`.
138+
135139
Valid keyword arguments are:
136140
137141
%(Text:kwdoc)s
@@ -956,11 +960,13 @@ def set_color(self, color):
956960

957961
def set_horizontalalignment(self, align):
958962
"""
959-
Set the horizontal alignment to one of
963+
Set the horizontal alignment relative to the anchor point.
964+
965+
See also :doc:`/gallery/text/text_alignment`.
960966
961967
Parameters
962968
----------
963-
align : {'center', 'right', 'left'}
969+
align : {'left', 'center', 'right'}
964970
"""
965971
_api.check_in_list(['center', 'right', 'left'], align=align)
966972
self._horizontalalignment = align
@@ -1194,11 +1200,13 @@ def set_transform_rotates_text(self, t):
11941200

11951201
def set_verticalalignment(self, align):
11961202
"""
1197-
Set the vertical alignment.
1203+
Set the vertical alignment relative to the anchor point.
1204+
1205+
See also :doc:`/gallery/text/text_alignment`.
11981206
11991207
Parameters
12001208
----------
1201-
align : {'center', 'top', 'bottom', 'baseline', 'center_baseline'}
1209+
align : {'bottom', 'baseline', 'center', 'center_baseline', 'top'}
12021210
"""
12031211
_api.check_in_list(
12041212
['top', 'bottom', 'center', 'baseline', 'center_baseline'],

0 commit comments

Comments
 (0)
0