- Notifications
You must be signed in to change notification settings - Fork 908
/
Copy pathtext-alignments.py
75 lines (60 loc) · 2.37 KB
/
text-alignments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -----------------------------------------------------------------------------
# Matplotlib cheat sheet
# Released under the BSD License
# -----------------------------------------------------------------------------
importpathlib
importnumpyasnp
importmatplotlib.pyplotasplt
ROOT_DIR=pathlib.Path(__file__).parent.parent
dpi=100
fig=plt.figure(figsize=(4.25, 1.5), dpi=dpi)
ax=fig.add_axes([0, 0, 1, 1], frameon=False,
xlim=(0, 4.25), ylim=(0, 1.5), xticks=[], yticks=[])
fontsize=48
renderer=fig.canvas.get_renderer()
horizontalalignment="left"
verticalalignment="center"
position= (0.25, 1.5/2)
color="0.25"
# Compute vertical and horizontal alignment offsets
text=ax.text(0, 0, "Matplotlib", fontsize=fontsize)
yoffset= {}
foralignmentin ["top", "center", "baseline", "bottom"]:
text.set_verticalalignment(alignment)
y=text.get_window_extent(renderer).y0/dpi
yoffset[alignment] =y
xoffset= {}
foralignmentin ["left", "center", "right"]:
text.set_horizontalalignment(alignment)
x=text.get_window_extent(renderer).x0/dpi
xoffset[alignment] =x
# Actual positioning of the text
text.set_horizontalalignment(horizontalalignment)
text.set_verticalalignment(verticalalignment)
text.set_position(position)
forname, yinyoffset.items():
y=position[1] -y+yoffset[verticalalignment]
plt.plot([0.1, 3.75], [y, y], linewidth=0.5, color=color)
plt.text(3.75, y, " "+name, color=color,
ha="left", va="center", size="x-small")
forname, xinxoffset.items():
x=position[0] -x+xoffset[horizontalalignment]
plt.plot([x, x], [0.25, 1.25], linewidth=0.5, color=color)
plt.text(x, 0.24, name, color=color,
ha="center", va="top", size="x-small")
P= []
forxinxoffset.values():
x=position[0] -x+xoffset[horizontalalignment]
foryinyoffset.values():
y=position[1] -y+yoffset[verticalalignment]
P.append((x, y))
P=np.array(P)
ax.scatter(P[:, 0], P[:, 1], s=10, zorder=10,
facecolor="white", edgecolor=color, linewidth=0.75)
epsilon=0.05
plt.text(P[3, 0]+epsilon, P[3, 1]-epsilon, "(0,0)",
color=color, ha="left", va="top", size="x-small")
plt.text(P[8, 0]-epsilon, P[8, 1]+epsilon, "(1,1)",
color=color, ha="right", va="bottom", size="x-small")
fig.savefig(ROOT_DIR/"figures/text-alignments.pdf")
# plt.show()