首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在matplotlib中悬停在垂直跨度区域上显示标签

在matplotlib中悬停在垂直跨度区域上显示标签
EN

Stack Overflow用户
提问于 2019-12-20 19:42:10
回答 1查看 247关注 0票数 3

当我将鼠标悬停在跨度区域上时,标签仅沿跨度区域的侧面显示,而不是沿整个区域显示。

我希望当我在标签上悬停时,整个区域都能看到它。我如何实现这个逻辑?

代码语言:javascript
复制
import matplotlib.pyplot as plt
import mplcursors


plt.axvspan(2,3,gid='yes',alpha=0.3,label = 'y')


mplcursors.cursor(hover=True).connect(
      "add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))


plt.show()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-20 23:02:37

我不知道为什么mplcursors不能在问题中的代码中工作;但这里是如何在悬停轴跨度时显示注释(没有mplcursor):

代码语言:javascript
复制
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
span = ax.axvspan(2,3,gid='yes',alpha=0.3,label = 'y')

annot = ax.annotate("Y", xy=(0,0), xytext=(20,20), textcoords="offset points",
                    bbox=dict(boxstyle="round", fc="w"),
                    arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)

def hover(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        cont, ind = span.contains(event)
        if cont:
            annot.xy = (event.xdata, event.ydata)
            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()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59424737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档