2013-04-08 194 views
7

我正在用matplotlib绘制一个2D数组。在窗口的右下角显示光标的x和y坐标。我怎样才能向这个状态栏添加关于光标下方数据的信息,例如,而不是'x = 439.501 y = 317.744'它会显示'x,y:[440,318] data:100'?我能以某种方式让我的手在这个导航工具栏上,并写下我自己的信息来显示吗?将信息添加到matplotlib导航工具栏/状态栏?

我已经设法为'button_press_event'添加我自己的事件处理程序,以便在终端窗口上打印数据值,但是这种方法只需要大量的鼠标点击和洪水交互式会话。

+0

可能重复(HTTP://计算器。 com/questions/14754931/matplotlib-values-under-cursor) – tacaswell 2013-04-08 14:10:32

+0

也http://stackoverflow.com/questions/14349289/in-a-matplotlib-figure-window-with-imshow-how-can-i-remove-隐藏或重新定义 – tacaswell 2013-04-08 14:11:21

回答

10

您只需重新指定ax.format_coord,该回调用于绘制该标签。

参见this example从文档,以及 In a matplotlib figure window (with imshow), how can I remove, hide, or redefine the displayed position of the mouse?matplotlib values under cursor

(代码直接从例如抬起)的[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() 
+0

所有谷歌搜索后,我仍然没有找到这一个!感谢您的解决方案! – northaholic 2013-04-09 06:45:51

+0

@northaholic如果这解决了你的问题,你可以接受它(左边的大灰色复选标记) – tacaswell 2013-04-09 14:52:19