我正在尝试创建一个交互式的hexbin情节。 用户应该能够遍历每个十六进制并查看注释值。 到目前为止,我无法为hexbin获得正确的注释,但是当对散点图使用相同的方法时,效果很好。 看起来hexbin的事件为触发事件提供了错误的索引,我该如何修复
from matplotlib.pyplot import figure, show
import matplotlib.pyplot as plt
import numpy as np;
np.random.seed(1)
x = np.random.rand(10)
y = np.random.rand(10)
c = np.random.randint(1,4,size=10)
norm = plt.Normalize(1,4)
cmap = plt.cm.RdYlGn
fig = figure()
ax = fig.add_subplot(111)
# sc = ax.scatter(x,y,c=c, s=100, cmap=cmap, norm=norm) # this works
sc = ax.hexbin(x, y, C=c,picker=True)
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,c[ind["ind"]]))))
annot.set_text(text)
def hover(event):
vis = annot.get_visible()
if event.inaxes == ax:
cont, ind = sc.contains(event)
print(cont)
print(ind)
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)
show()
目前没有回答
相关问题 更多 >
编程相关推荐