如何在Matplotlib中控制鼠标悬停文本 [英] How to control mouseover text in matplotlib
问题描述
当您将鼠标悬停在使用 imshow 显示的图像上时,您可以将鼠标悬停在该图像上以检查其 RGB 值.matplotlib窗口的右下角(带有工具栏的共享空间)显示了所指向像素的图像坐标和RGB值:
When you mouseover an image shown with imshow, you can mouseover the image to inspect its RGB values. The bottom-right corner of the matplotlib window (sharing space with the toolbar), shows the image coordinates and RGB values of the pixel being pointed at:
x = 274.99 y = 235.584 [.241, .213, .203]
但是,当我将鼠标悬停在颤动图上时,它仅显示指针的x和y坐标,而没有显示所指向的2D向量的值.有没有办法让向量值显示出来?
However, when I mouseover a quiver plot, it only shows the x and y coords of the pointer, but not the value of the 2D vector being pointed at. Is there a way to get the vector values to show up?
如果我只知道如何在 matplotlib 窗口中设置该位文本,我就可以编写自定义鼠标事件处理程序.
I would be fine with writing a custom mouse event handler, if I only knew how to set that bit of text in the matplotlib window.
推荐答案
有时默认情况下不存在有关颜色值的信息.实际上,我认为当前版本基于Stackoverflow上有关该功能的一些问题的一些代码.
There were times when the information about the color value was not present by default. In fact I think the current version is based on some code that came up in Stackoverflow on some question about that feature.
我很快就找到了这两个问题:
I quickly found those two questions:
这个想法是改变鼠标悬停在轴上时调用的函数.此函数存储在 ax.format_coord
中.因此,一个可能的解决方案是编写自定义函数以根据输入坐标返回所需的输出,例如像
The idea would be to change the function that is called when the mouse hovers the axes. This function is stored in ax.format_coord
. So a possible solution is to write your custom function to return the desired output based on the input coordinates, e.g. something like
def format_coord(x, y):
try:
z = # get value depending on x,y, e.g. via interpolation on grid
# I can't fill this because the kind of data is unknown here
return "x: {}, y: {}, z: {}".format(x,y,z)
except:
return "x: {}, y: {}".format(x,y)
ax.format_coord = format_coord
这篇关于如何在Matplotlib中控制鼠标悬停文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!