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 3224994

Browse filesBrowse 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 3224994
Copy full SHA for 3224994

File tree

Expand file treeCollapse file tree

1 file changed

+38
-69
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+38
-69
lines changed

‎examples/pyplots/text_layout.py

Copy file name to clipboardExpand all lines: examples/pyplots/text_layout.py
+38-69Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.