问题
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]
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?
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.
回答1:
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:
- matplotlib values under cursor
- Interactive pixel information of an image in Python?
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
来源:https://stackoverflow.com/questions/46531243/how-to-control-mouseover-text-in-matplotlib