将信息添加到Matplotlib导航工具栏/状态栏?

13
我正在使用Matplotlib绘制2D数组。在窗口的右下角显示鼠标的x和y坐标。如何向此状态栏添加有关光标下方数据的信息,例如,而不是“x=439.501 y=317.744”,它将显示“x,y: [440,318] data: 100”?我是否可以获取导航工具栏并编写自己的消息以显示?
我已成功添加了自己的“button_press_event”事件处理程序,以便将数据值打印到终端窗口,但这种方法需要大量点击鼠标并淹没了交互式会话。

可能是matplotlib鼠标下的值的重复问题。 - tacaswell
也可以在https://dev59.com/hWYq5IYBdhLWcg3wmRya找到相关信息。 - tacaswell
1个回答

19
你只需要重新分配ax.format_coord,这是用于绘制标签的回调函数。请参阅文档中的此示例,以及在matplotlib图形窗口中(使用imshow),如何删除、隐藏或重新定义鼠标显示的位置?matplotlib下鼠标值。(直接从示例中提取的代码)
"""
Show how to modify the coordinate formatter to report the image "z"
value of the nearest pixel given x and y
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

X = 10*np.random.rand(5,3)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(X, cmap=cm.jet, interpolation='nearest')

numrows, numcols = X.shape
def format_coord(x, y):
    col = int(x+0.5)
    row = int(y+0.5)
    if col>=0 and col<numcols and row>=0 and row<numrows:
        z = X[row,col]
        return 'x=%1.4f, y=%1.4f, z=%1.4f'%(x, y, z)
    else:
        return 'x=%1.4f, y=%1.4f'%(x, y)

ax.format_coord = format_coord
plt.show()

经过那么多的谷歌搜索,我仍然没有找到这一个!感谢你提供的解决方案! - northaholic
@northaholic 如果这个解决了你的问题,你可以接受它(左边的大灰色勾号)吗? - tacaswell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接