How do I change the data display format for a imshow plot in matplotlib?

北城以北 提交于 2019-12-12 02:49:21

问题


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

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:


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



回答2:


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()



回答3:


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.




回答4:


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)])


来源:https://stackoverflow.com/questions/36244787/how-do-i-change-the-data-display-format-for-a-imshow-plot-in-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!