13

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?

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.

northaholic
  • 143
  • 1
  • 6
  • possible duplicate of [matplotlib values under cursor](http://stackoverflow.com/questions/14754931/matplotlib-values-under-cursor) – tacaswell Apr 08 '13 at 14:10
  • also http://stackoverflow.com/questions/14349289/in-a-matplotlib-figure-window-with-imshow-how-can-i-remove-hide-or-redefine – tacaswell Apr 08 '13 at 14:11

1 Answers1

19

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

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()
Community
  • 1
  • 1
tacaswell
  • 84,579
  • 22
  • 210
  • 199