1

Or more specifically, how can I change the [659] on the upper-right corner to '659 degrees' or something like that ?

enter image description here

I have checked all the threads mentioned in the following reply: matplotlib values under cursor. However, all of them seem to address the x,y location of the cursor. I am interested in changing the data-value. I could not find a reply or related documentation in the api.

I have tried both format_coord(x, y) and format_cursor_data(data) but neither of them seem to be working.

Thanks, Sarith

PS: My code is in multiple modules and is a part of gui application. I can share relevant sections if that would be of any help in answering this.

1
  • The weird thing is that usually it doesn' even show x and y coordinates, so you must already have a function for that.. Commented Mar 27, 2016 at 7:18

4 Answers 4

1

One line solution:

ax.format_coord = lambda x, y: 'x={:.2f}, y={:.2f}, z={:.2f}'.format(x,y,data[int(y + 0.5),int(x + 0.5)])
Sign up to request clarification or add additional context in comments.

Comments

1

I had the same problem (I wanted to get rid of the data and send it to somewhere else in a tkinter widget). I figured out the ax.format_coord was'nt being called, the one you have to change is the one at matplotlib.artist.Artist this worked for me:

    def format_cursor_data(self,data):            
        return 'new_data'
    matplotlib.artist.Artist.format_cursor_data=format_cursor_data

1 Comment

You also can reassign this function onto AxesImage object returned from imshow: img = p.imshow(my_image); img.format_cursor_data = format_cursor_data
0

By modifying an example from matplotlib I got this code:

This displays x,y, and z value with a degrees after z.

You should be able to easily modify it, or copy the relevant functions to make it work on your side.

You said you already tried format_coord, maybe you forgot to set the funtion? (second last line)

"""
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, ax = plt.subplots()
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:

        #get z from your data, given x and y
        z = X[row, col]

        #this only leaves two decimal points for readability
        [x,y,z]=map("{0:.2f}".format,[x,y,z])

        #change return string of x,y and z to whatever you want
        return 'x='+str(x)+', y='+str(y)+', z='+str(z)+" degrees"
    else:
        return 'x=%1.4f, y=%1.4f' % (x, y)

#Set the function as the one used for display
ax.format_coord = format_coord
plt.show()

6 Comments

Hi, thanks for replying. But this doesn't do it. It just displays something like "x=0.01, y=0.02 z=0.40 degrees [3.4]" (which is actually confusing). What I meant was something like than "x=0.01, y=0.02, 3.4 degrees". Perhaps I am asking for too much :/
Where do you get the [3.4] from? Is it automatically generated? Try changing return 'x='+str(x)+', y='+str(y)+', z='+str(z)+" degrees" to return 'x='+str(x)+', y='+str(y)+', z='+"Deg: ". This should then at least display "x=0.01, y=0.02, Deg: [3.4] "
Well, it just shows up on its own. I am using matplotlib 1.5.1. Here is the screenshot from your code: dropbox.com/s/aim7h7o866ht4oq/Untitled.jpg?dl=0
@SarithSubramaniam well that is... weird. You could remove str(z) resulting in "x=0.01, y=0.02, degrees [3.4] ". I would guess there is a code fragment somewhere which causes this, because I can't get it to appear on my matplotlib :/
This is one of the new features in 1.5. The [] are hard-coded into the move callback in the Toolbar (github.com/matplotlib/matplotlib/blob/master/lib/matplotlib/… ). You could sub-class and override that method or monkey-patch the format_cursor_data method on your image artist.
|
0

Emmm, Sarith, I am also facing the problem but a little trick helped me out. I still used this function:

your_imshow.format_coord = lambda x, y: 'x={:.5f}, y={:.2f}, amplitude='.format(x,y)

It pretends to add label before the bracket. Yeah, it is an easy but not essential way to change the presentation form, but it works to me. I hope this could also benefit others.

Comments

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.