将信息添加到matplotlib导航工具栏/状态栏? [英] Add information to matplotlib Navigation toolbar/status bar?

查看:54
本文介绍了将信息添加到matplotlib导航工具栏/状态栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 matplotlib 绘制一个二维数组.在窗口的右下角显示了光标的x和y坐标.如何在状态栏上添加有关光标下方数据的信息,例如,而不是"x = 439.501 y = 317.744",它将显示"x,y:[440,318] data:100"?我能以某种方式把手放在此导航工具栏上并写下自己要显示的消息吗?

I'm plotting a 2D-array with matplotlib. On the lower right corner of the window the x and y coordinates of the cursor are showed. How can I add information to this status bar about the data underneath the cursor, e.g., instead of 'x=439.501 y=317.744' it would show 'x,y: [440,318] data: 100'? Can I somehow get my hands on this Navigation toolbar and write my own message to be showed?

我已经设法为button_press_event"添加了自己的事件处理程序,以便将数据值打印在终端窗口上,但这种方法只需要多次点击鼠标并淹没交互会话.

I've managed to add my own event handler for 'button_press_event', so that the data value is printed on the terminal window, but this approach just requires a lot of mouse clicking and floods the interactive session.

推荐答案

您只需重新分配 ax.format_coord,用于绘制该标签的回调.

You simply need to re-assign ax.format_coord, the call back used to draw that label.

请参阅文档中的此示例,以及在matplotlib图形窗口(带有imshow)中,如何删除,隐藏或重新定义鼠标的显示位置?光标下的matplotlib值

See this example from the documentation, as well as In a matplotlib figure window (with imshow), how can I remove, hide, or redefine the displayed position of the mouse? and matplotlib values under cursor

(直接从示例中提取的代码)

(code lifted directly from example)

"""
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()

这篇关于将信息添加到matplotlib导航工具栏/状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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