【发布时间】:2019-06-26 15:48:54
【问题描述】:
我从this other answer中的代码开始,我修改了一部分,以便在悬停到绘图中心时始终显示标签:
...
annot = ax.annotate("", xy=(0,0), xytext=(0,0),textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
xmid = np.mean(ax.get_xlim())
ymid = np.mean(ax.get_ylim())
offset = 20
def update_annot(ind):
pos = sc.get_offsets()[ind["ind"][0]]
annot.xy = pos
annot.xytext = (-offset if pos[0] > xmid else offset,
-offset if pos[1] > ymid else offset)
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)
annot.set_ha("right" if pos[0] > xmid else "left")
annot.set_va("top" if pos[1] > ymid else "bottom")
...
产生以下输出:
还有两个例子here 和here。这不是预期的输出。箭头方向正确,文本接近所需方向,但它与数据点对齐,而不是与箭头末端对齐。
我期望得到的是在调用ax.annotate 时使用相同的参数实现的结果(无需交互式修改注释),即以下行为:
【问题讨论】:
-
基本上this answer 已经回答了这个问题。它会被视为重复吗?
-
是的!我错过了这个答案。我应该删除它还是将其标记为重复会更好?
-
标记为重复就好了;这增加了人们在搜索问题时找到答案的机会。
标签: python matplotlib