forked from rougier/matplotlib-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference-alignments.py
71 lines (59 loc) · 2.32 KB
/
reference-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
# ----------------------------------------------------------------------------
# Title: Scientific Visualisation - Python & Matplotlib
# Author: Nicolas P. Rougier
# License: BSD
# ----------------------------------------------------------------------------
import numpy as np
import matplotlib.pyplot as plt
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 = {}
for alignment in ["top", "center", "baseline", "bottom"]:
text.set_verticalalignment(alignment)
y = text.get_window_extent(renderer).y0/dpi
yoffset[alignment] = y
xoffset = {}
for alignment in ["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)
for name,y in yoffset.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")
for name,x in xoffset.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 = []
for x in xoffset.values():
x = position[0] - x + xoffset[horizontalalignment]
for y in yoffset.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")
plt.savefig("text-alignments.pdf", dpi=600)
plt.show()