Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add information to matplotlib Navigation toolbar/status bar?

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.

like image 402
northaholic Avatar asked Apr 08 '13 09:04

northaholic


People also ask

Is PLT show () blocking?

show() and plt. draw() are unnecessary and / or blocking in one way or the other.

How do I make matplotlib zoomable?

Press the right mouse button to zoom, dragging it to a new position. The x axis will be zoomed in proportionately to the rightward movement and zoomed out proportionately to the leftward movement. The same is true for the y axis and up/down motions.


1 Answers

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()
like image 82
tacaswell Avatar answered Sep 17 '22 20:09

tacaswell