10000 Simplify text_layout example. · matplotlib/matplotlib@2b4f2dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b4f2dc

Browse files
committed
Simplify text_layout example.
By working with figure-level artists rather than axes-level ones (where the axes cover the whole figure...) we don't have do specify the transform again and again.
1 parent 49decb4 commit 2b4f2dc

File tree

1 file changed

+35
-69
lines changed

1 file changed

+35
-69
lines changed

examples/pyplots/text_layout.py

Lines changed: 35 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,48 @@
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+
fig.text(left, bottom, 'left top',
25+
horizontalalignment='left', verticalalignment='top')
26+
fig.text(left, bottom, 'left bottom',
27+
horizontalalignment='left', verticalalignment='bottom')
28+
fig.text(right, top, 'right bottom',
29+
horizontalalignment='right', verticalalignment='bottom')
30+
fig.text(right, top, 'right top',
31+
horizontalalignment='right', verticalalignment='top')
32+
fig.text(right, bottom, 'center top',
33+
horizontalalignment='center', verticalalignment='top')
34+
fig.text(left, 0.5*(bottom+top), 'right center',
35+
horizontalalignment='right', verticalalignment='center',
36+
rotation='vertical')
37+
fig.text(left, 0.5*(bottom+top), 'left center',
38+
horizontalalignment='left', verticalalignment='center',
39+
rotation='vertical')
40+
fig.text(0.5*(left+right), 0.5*(bottom+top), 'middle',
41+
horizontalalignment='center', verticalalignment='center',
42+
fontsize=20, color='red')
43+
fig.text(right, 0.5*(bottom+top), 'centered',
44+
horizontalalignment='center', verticalalignment='center',
45+
rotation='vertical')
46+
fig.text(left, top, 'rotated\nwith newlines',
47+
horizontalalignment='center', verticalalignment='center',
48+
rotation=45)
8249

83-
ax.set_axis_off()
8450
plt.show()
8551

8652
#############################################################################
@@ -94,5 +60,5 @@
9460
# in this example:
9561

9662
import matplotlib
97-
matplotlib.axes.Axes.text
98-
matplotlib.pyplot.text
63+
matplotlib.figure.Figure.add_artist
64+
matplotlib.figure.Figure.text

0 commit comments

Comments
 (0)
0