Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a matplotlib figure window (with imshow), how can I remove, hide, or redefine the displayed position of the mouse? [duplicate]

I am using ipython with matplotlib, and I show images in this way:

(started up with: ipython --pylab)

figure()  
im = zeros([256,256]) #just a stand-in for my real images   
imshow(im)

Now, as I move the cursor over the image, I see the location of the mouse displayed in the lower left corner of the figure window. The numbers displayed are x = column number, y = row number. This is very plot-oriented rather than image-oriented. Can I modify the numbers displayed?

  1. My first choice would be to display x = row number*scalar, y = column number*scalar
  2. My second choice would be to display x = row number, y = column number
  3. My third choice is to not display the numbers for the mouse location at all

Can I do any of these things? I'm not even sure what to call that little mouse-over test display widget. Thanks!

like image 301
Becca Avatar asked Jan 16 '13 00:01

Becca


1 Answers

You can do this quite simply on a per axis basis by simply re-assigning format_coord of the Axes object, as shown in the examples.

format_coord is any function which takes 2 arguments (x,y) and returns a string (which is then displayed on the figure.

If you want to have no display simply do:

ax.format_coord = lambda x, y: ''

If you want just the row and column (with out checking)

scale_val = 1
ax.format_coord = lambda x, y: 'r=%d,c=%d' % (scale_val * int(x + .5), 
                                             scale_val * int(y + .5))

If you want to do this on every iimage you make, simply define the wrapper function

def imshow(img, scale_val=1, ax=None, *args, **kwargs):
    if ax is None:
         ax = plt.gca()
    im = ax.imshow(img, *args, **kwargs)
    ax.format_coord = lambda x, y: 'r=%d,c=%d' % (scale_val * int(x + .5), 
                                             scale_val * int(y + .5))
    ax.figure.canvas.draw()
    return im

which with out much testing I think should more-or-less be drop-in replacement for plt.imshow

like image 58
tacaswell Avatar answered Sep 23 '22 11:09

tacaswell