Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 633a83c

Browse files
authored
Merge pull request #14805 from anntzer/text_layout
Simplify text_layout example.
2 parents e7841c2 + 3224994 commit 633a83c

File tree

1 file changed

+38
-69
lines changed

1 file changed

+38
-69
lines changed

examples/pyplots/text_layout.py

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,51 @@
55
66
Create text with different alignment and rotation.
77
"""
8+
89
import matplotlib.pyplot as plt
910
import matplotlib.patches as patches
1011

11-
# build a rectangle in axes coords
12+
fig = plt.figure()
13+
1214
left, width = .25, .5
1315
bottom, height = .25, .5
1416
right = left + width
1517
top = bottom + height
1618

17-
fig = plt.figure()
18-
ax = fig.add_axes([0,0,1,1])
19-
20-
# axes coordinates are 0,0 is bottom left and 1,1 is upper right
21-
p = patches.Rectangle(
22-
(left, bottom), width, height,
23-
fill=False, transform=ax.transAxes, clip_on=False
24-
)
25-
26-
ax.add_patch(p)
27-
28-
ax.text(left, bottom, 'left top',
29-
horizontalalignment='left',
30-
verticalalignment='top',
31-
transform=ax.transAxes)
32-
33-
ax.text(left, bottom, 'left bottom',
34-
horizontalalignment='left',
35-
verticalalignment='bottom',
36-
transform=ax.transAxes)
37-
38-
ax.text(right, top, 'right bottom',
39-
horizontalalignment='right',
40-
verticalalignment='bottom',
41-
transform=ax.transAxes)
42-
43-
ax.text(right, top, 'right top',
44-
horizontalalignment='right',
45-
verticalalignment='top',
46-
transform=ax.transAxes)
47-
48-
ax.text(right, bottom, 'center top',
49-
horizontalalignment='center',
50-
verticalalignment='top',
51-
transform=ax.transAxes)
52-
53-
ax.text(left, 0.5*(bottom+top), 'right center',
54-
horizontalalignment='right',
55-
verticalalignment='center',
56-
rotation='vertical',
57-
transform=ax.transAxes)
58-
59-
ax.text(left, 0.5*(bottom+top), 'left center',
60-
horizontalalignment='left',
61-
verticalalignment='center',
62-
rotation='vertical',
63-
transform=ax.transAxes)
64-
65-
ax.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
66-
horizontalalignment='center',
67-
verticalalignment='center',
68-
fontsize=20, color='red',
69-
transform=ax.transAxes)
70-
71-
ax.text(right, 0.5*(bottom+top), 'centered',
72-
horizontalalignment='center',
73-
verticalalignment='center',
74-
rotation='vertical',
75-
transform=ax.transAxes)
76-
77-
ax.text(left, top, 'rotated\nwith newlines',
78-
horizontalalignment='center',
79-
verticalalignment='center',
80-
rotation=45,
81-
transform=ax.transAxes)
19+
# Draw a rectangle in figure coordinates ((0, 0) is bottom left and (1, 1) is
20+
# upper right).
21+
p = patches.Rectangle((left, bottom), width, height, fill=False)
22+
fig.add_artist(p)
23+
24+
# Figure.text (aka. plt.figtext) behaves like Axes.text (aka. plt.text), with
25+
# the sole exception that the coordinates are relative to the figure ((0, 0) is
26+
# bottom left and (1, 1) is upper right).
27+
fig.text(left, bottom, 'left top',
28+
horizontalalignment='left', verticalalignment='top')
29+
fig.text(left, bottom, 'left bottom',
30+
horizontalalignment='left', verticalalignment='bottom')
31+
fig.text(right, top, 'right bottom',
32+
horizontalalignment='right', verticalalignment='bottom')
33+
fig.text(right, top, 'right top',
34+
horizontalalignment='right', verticalalignment='top')
35+
fig.text(right, bottom, 'center top',
36+
horizontalalignment='center', verticalalignment='top')
37+
fig.text(left, 0.5*(bottom+top), 'right center',
38+
horizontalalignment='right', verticalalignment='center',
39+
rotation='vertical')
40+
fig.text(left, 0.5*(bottom+top), 'left center',
41+
horizontalalignment='left', verticalalignment='center',
42+
rotation='vertical')
43+
fig.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
44+
horizontalalignment='center', verticalalignment='center',
45+
fontsize=20, color='red')
46+
fig.text(right, 0.5*(bottom+top), 'centered',
47+
horizontalalignment='center', verticalalignment='center',
48+
rotation='vertical')
49+
fig.text(left, top, 'rotated\nwith newlines',
50+
horizontalalignment='center', verticalalignment='center',
51+
rotation=45)
8252

83-
ax.set_axis_off()
8453
plt.show()
8554

8655
#############################################################################
@@ -94,5 +63,5 @@
9463
# in this example:
9564

9665
import matplotlib
97-
matplotlib.axes.Axes.text
98-
matplotlib.pyplot.text
66+
matplotlib.figure.Figure.add_artist
67+
matplotlib.figure.Figure.text

0 commit comments

Comments
 (0)