-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhover.py
56 lines (43 loc) · 1.41 KB
/
hover.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
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(1)
x = np.random.rand(15)
y = np.random.rand(15)
# names = np.array(list("ABCDEFGHIJKLMNO"))
names = list("ABCDEFGHIJKLMNO")
c = np.random.randint(1, 5, size=15)
print(c)
norm = plt.Normalize(1, 4)
print(norm.vmin)
print(norm.vmax)
cmap = plt.cm.RdYlGn
# cmap = "RdYlGn"
# cmap = plt.cm.Set1
fig, ax = plt.subplots()
sc = plt.scatter(x, y, c=c, s=100, cmap=cmap, norm=norm)
annot = ax.annotate("", xy=(0, 0), xytext=(20, 20),textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
def update_annot(ind):
pos = sc.get_offsets()[ind["ind"][0]]
annot.xy = pos
text = "{}, {}".format(" ".join(list(map(str, ind["ind"]))),
" ".join([names[n] for n in ind["ind"]]))
annot.set_text(text)
annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]])))
annot.get_bbox_patch().set_alpha(0.4)
def hover(event):
vis = annot.get_visible()
if event.inaxes == ax:
cont, ind = sc.contains(event)
if cont:
update_annot(ind)
annot.set_visible(True)
fig.canvas.draw_idle()
else:
if vis:
annot.set_visible(False)
fig.canvas.draw_idle()
fig.canvas.mpl_connect("motion_notify_event", hover)
plt.show()