2013-04-08 195 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