检测鼠标悬停在图例上,并在 matplotlib 中显示工具提示(标签/注释)? [英] Detect mouse hover over legend, and show tooltip (label/annotation) in matplotlib?

查看:117
本文介绍了检测鼠标悬停在图例上,并在 matplotlib 中显示工具提示(标签/注释)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过

问题是:如果我移动鼠标指针,所以它将鼠标悬停在图例条目上,那么由于事件的打印输出,我得到的全部是:

<代码>....motion_notify_event:xy =(175,652)xydata =(无,无)按钮=无dblclick =错误的inaxes =无motion_notify_event:xy =(174,652)xydata =(无,无)按钮=无dblclick =错误的inaxes =无motion_notify_event: xy=(173, 652) xydata=(None, None) button=None dblclick=False inaxes=Nonemotion_notify_event: xy=(172, 652) xydata=(None, None) button=None dblclick=False inaxes=None

...这是有道理的,因为我故意将图例放在情节之外.

但是,现在我不知道如何获得对图例条目的引用?我想做的基本上是获得图例条目的引用,以便我可以在工具提示"(即本例中为Matplotlib)中编写与图例标签(此处为我的情节")相同的文本.注释)后跟一些其他文本;然后,一旦鼠标离开图例条目的区域,工具提示/注释就会消失.

我可以使用 Matplotlib 实现这一点吗?如果可以,如何实现?

解决方案

你可以这样做:

def onhover(event):如果leg.get_window_extent().包含(event.x,event.y):打印(事件,传说中!做点什么!")xdata = np.arange(0,101,1)#0至100,都包含ydata1 = np.sin(0.01 * xdata * np.pi/2)无花果,ax1 = plt.subplots(1,1,figsize =(9,6),dpi = 120)pl11,= ax1.plot(xdata,ydata1,color ="Red",label =我的情节")leg = ax1.legend(ncol = 1,bbox_to_anchor =(0,1.01),loc =左下",borderaxespad = 0,prop = {'size':8})fig.canvas.mpl_connect('motion_notify_event', onhover)plt.show()

I have seen Possible to make labels appear when hovering over a point in matplotlib? - but unfortunately, it does not help me with this specific case.

Consider this example:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import matplotlib
print("matplotlib.__version__ {}".format(matplotlib.__version__))
import matplotlib.pyplot as plt
import numpy as np


def onhover(event, fig, axes):
  print(event)

def main():
  xdata = np.arange(0, 101, 1) # 0 to 100, both included
  ydata1 = np.sin(0.01*xdata*np.pi/2)

  fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), dpi=120)
  fig.subplots_adjust(hspace=0)

  pl11, = ax1.plot(xdata, ydata1, color="Red", label="My plot")

  leg = ax1.legend(ncol=1, bbox_to_anchor=(0,1.01), loc="lower left", borderaxespad=0, prop={'size': 8})

  fig.canvas.mpl_connect('motion_notify_event', lambda event: onhover(event, fig, (ax1,) ))
  plt.show()

# ENTRY POINT
if __name__ == '__main__':
  main()

This results with the following plot:

The thing is: if I move the mouse pointer, so it hovers over the legend entry, all I get as the printout in the event is:

....
motion_notify_event: xy=(175, 652) xydata=(None, None) button=None dblclick=False inaxes=None
motion_notify_event: xy=(174, 652) xydata=(None, None) button=None dblclick=False inaxes=None
motion_notify_event: xy=(173, 652) xydata=(None, None) button=None dblclick=False inaxes=None
motion_notify_event: xy=(172, 652) xydata=(None, None) button=None dblclick=False inaxes=None

... which makes sense, as I've deliberately placed the legend outside of the plot.

However, now I do not know how I can get a reference to the legend entry? What I would like to do, is basically get the reference to the legend entry, so that I could write the same text as the legend label (here "My plot") in a "tooltip" (that is, in this case, Matplotlib annotation) followed by some other text; and then, once the mouse leaves the region of the legend entry, the tooltip/annotation should disappear.

Can I achieve this with Matplotlib - and if so, how?

解决方案

You could do something like that:

def onhover(event):
    if leg.get_window_extent().contains(event.x,event.y):
        print(event, "In legend! do something!")


xdata = np.arange(0, 101, 1) # 0 to 100, both included
ydata1 = np.sin(0.01*xdata*np.pi/2)

fig, ax1 = plt.subplots(1, 1, figsize=(9, 6), dpi=120)
pl11, = ax1.plot(xdata, ydata1, color="Red", label="My plot")
leg = ax1.legend(ncol=1, bbox_to_anchor=(0,1.01), loc="lower left", borderaxespad=0, prop={'size': 8})

fig.canvas.mpl_connect('motion_notify_event', onhover)
plt.show()

这篇关于检测鼠标悬停在图例上,并在 matplotlib 中显示工具提示(标签/注释)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆