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.

2

1 Answer 1

20

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()
Sign up to request clarification or add additional context in comments.

2 Comments

after all that googling, I still didn't find this one! thanks for the solution!
@northaholic If this solved your problem can you accept it (the big gray check mark on the left)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.